Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML
 
 
Hot Web Programming Job

 

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML

[an error occurred while processing this directive]

Java: JLabel

Labels display fixed text or images on a GUI as information to the user, for example, as a label in front of a a JTextField, etc. You can have text (including HTML), an image, or both on a JLabel. A JLabel has a transparent background, so it will always match the container it is in.

JLabel Constructors

Assume the following declarations.

   String text;
   Icon   image;
   int    alignment; // JLabel.LEFT, JLabel.Center, or JLabel.RIGHT.
   
JLabel yourLabel = new JLabel(text);
JLabel yourLabel = new JLabel(text, alignment);
JLabel yourLabel = new JLabel(image);
JLabel yourLabel = new JLabel(image, alignment);
JLabel yourLabel = new JLabel(text, image, alignment);

Java Idiom

Because there is usually no need to refer to a JLabel after it has been added to a container, it is common to combine creation and adding the JLabel in one statement. For example.

p.add(new JLabel("Enter your ID:", JLabel.RIGHT));

is the same as

JLabel idLabel = new JLabel("Enter your ID:", JLabel.RIGHT);
. . .
p.add(idLabel);

HTML in JLabels

You may put HTML text in a JLabel. In this case the text should begin with <html> and end with </html>.

JLabel font and color

The most user-friendly interfaces are usually obtained by using the default appearance (font, color, background), but there are cases where you want to change these.

Appearance: setting the font

The font of a JLabel can be changed like this.

JLabel title = new JLabel("Want a Raise?", JLabel.CENTER);
title.setFont(new Font("Serif", Font.BOLD, 48));

Appearance: setting the text color

Use the setForeground method to set the text color.

JLabel title = new JLabel("Want a Raise?", JLabel.CENTER);
title.setForeground(Color.white);

Appearance: setting the background color

Because a JLabel's background is transparent, there is no effect from using the setBackground method. To make a new background, you need to create a JPanel with the appropriate color and put the label on that. For example

JLabel title = new JLabel("Want a Raise?");
title.setForeground(Color.white);
JPanel titlePanel = new JPanel();
titlePanel.setBackground(Color.blue);
titlePanel.add(title);  // adds to center of panel's default BorderLayout.

JLabel for output

Why using JLabel for output is usually bad

It's possible to change the text of a JLabel, although this is not generally a good idea after the user interface is already displayed. For output JTextField is often a better choice. The use of JLabel for output is mentioned because some textbooks display output this way. Here are some reasons not to use it.

  • Can't copy to clipboard. The user can not copy text from a JLabel, but can from a JTextField.
  • Can't set background. Changing the background of individual components probably isn't a good idea, so this restriction on JLabels is not serious. You can change the background of a JTextField, for better or worse.
  • Text length. This is where there are some serious issues. You can always see the entire text in a JTextField, altho you might have to scroll it it's long. There are several possibilities with a JLabel. You may either not see all of the long text in a JLabel, or putting long text into a JLabel may cause the layout to be recomputed, resulting in a truly weird user experience.

Changing the text of a JLabel

Most JLabels are never changed, except for internationalization, and that is done before the user interface is shown. To change the text, use

yourLabel.setText(String newText);  // 

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (
post your own) View All Comments Latest 10 Comments:
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.