Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

Java: JLabel

JavaFAQ Home » Swing, AWT Go to all tips in Swing, AWT


Bookmark and Share 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 and end with .

Related pages

The most user-friendly interfaces are usually obtained by using the default appearance. Altho the default is the most common, you might want to change the font size in some situations. Look at JLabel font and color if you really must make changes. A more common way to control the appearance of a JLabel is using HTML (see above).

It's also possible to dynamically change the text of a JLabel, ie, use it for output. This is not generally a good idea because it may change the size of the JLabel and therefore require a change in the layout of components. Using a JTextField is much better. JLabel setting text has information for how to do it tho.


 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy