Reviews : Java Books :
Learning Java : Chapter 14: Using Swing Components

Title: Learning Java
ISBN: 1565927184
Order No 7184
US Price: $ 34.95
Publication Date: May 2000
Pages: 722
© O'Reilly & Associates, Inc.
Author's Top Ten Tips and Tricks

Buttons and Labels - Con't

There's even less to be said about JLabel components. They're just text strings or images housed in a component. There aren't any special events associated with labels; about all you can do is specify the text's alignment, which controls the position of the text within the label's display area. As with buttons, JLabels can be created with Icons if you want to create a picture label. The following code creates some labels with different options:

// default alignment (CENTER)
JLabel label1 = new JLabel("Lions");
 
// left aligned
JLabel label2 = new JLabel("Tigers", SwingConstants.LEFT);
 
//label with no text, default alignment
JLabel label3 = new JLabel(  );
 
// create image icon 
Icon icon = new ImageIcon("rhino.gif");
 
// create image label 
JLabel label4 = new JLabel(icon);
 
// assigning text to label3
label3.setText("and Bears");
 
// set alignment 
label3.setHorizontalAlignment(SwingConstants.RIGHT);

The alignment constants are defined in the SwingConstants interface.

Now we've built several labels, using a variety of constructors and several of the class's methods. To display the labels, just add them to a container by calling the container's add( ) method.

The other characteristics you might like to set on labels, such as changing their font or color, are accomplished using the methods of the Component class, JLabel's distant ancestor. For example, you can call setFont( ) and setColor( ) on a label, as with any other component.

Given that labels are so simple, why do we need them at all? Why not just draw a text string directly on the container object? Remember that a JLabel is a JComponent. That's important; it means that labels have the normal complement of methods for setting fonts and colors that we mentioned earlier, as well as the ability to be managed sensibly by a layout manager. Therefore, they're much more flexible than a text string drawn at an absolute location within a container.

Speaking of layouts--if you use the setText( ) method to change the text of your label, the label's preferred size may change. But the label's container will automatically lay out its components when this happens, so you don't have to worry about it.

Swing can interpret HTML-formatted text in JLabel and JButton labels. The following example shows how to create a button with HTML-formatted text:

JButton button = new JButton(
  "<html>"
  + "S<font size="-1">MALL<font size="+0"> "
  + "C<font size="-1">APITALS");

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.