JLabel Example ToolTip and Icon Swing

JLabel Example: In many cases, JLabel is used exactly like Label of AWT as a way to display text. But with JLabel we can have an image only or image with text. JLabel is also frequently used as a way to put an image in a display. JLabel does not generate any events. The label can be aligned in different ways like left, right, center etc.

New Features: We can set images, different borders, and text with HTML.

A JLabel has three major features that AWT Label does not:

1. The first is the ability to display images, usually by supplying an ImageIcon. The use of icons in JLabel is just like the use in JButton.

2. The second new feature is the ability to place borders around the labels.

3. The third new feature is the ability to use HTML to format the label. This lets us make multi-line labels, labels with mixed colors and fonts and various other fancy effects. This capability also applies to JButton. Although nice, this capability also has several significant limitations as all versions of JDK will not support, but supported by JDK1.2.2 and JDK1.3 and higher.

The following JLabel Example displays labels with images.
import javax.swing.* ;
import java.awt.*;  			// for Container class
import java.awt.event.*;  		// for window closing

public class JLabelDemo1 extends JFrame
  JLabel lab1, lab2, lab3;
  public JLabelDemo1()
    Container c = getContentPane();
    c.setLayout(new GridLayout(3, 1, 0, 5)); 
 			          // using JLabel constructor with a string argument
    lab1 = new JLabel("Hello World");                                                      
    c.add(lab1);                  // to add the label to the container 
		                  // using JLabel constructor with a string, icon and alignment arguments
    ImageIcon ic1 = new ImageIcon("bird4.gif");
    lab2 = new JLabel("How do you do?", ic1, SwingConstants.LEFT);
    lab2.setToolTipText("Well Wishes");
		                  // using JLabel constructor with no arguments. Properties are set later
    ImageIcon ic2 = new ImageIcon("bird5.gif");
    lab3 = new JLabel();
    lab3.setText("All are Okay");
    lab3.setToolTipText("Quite and Peaceful");
    lab3.setVerticalTextPosition(SwingConstants.BOTTOM);  // if kept in comments, icon and label come in one line. Now text comes below the image

    setTitle("Learning JLabel");                                               
  public static void main(String args[])
    new JLabelDemo1();                                                         

JLabel Example
Output screenshot on JLabel Example


setToolTipText(String message) method is inherited from JComponent class. All the subclasses of JComponent class can make use of this method. That is, JButton, JTextField etc. can make of this method. The message is displayed when we take the mouse cursor over the label. In the screen capture, "Well Wishes" is the tool tip displayed when the mouse cursor is taken over lab2 object of JLabel.

ImageIcon ic1 = new ImageIcon(“bird4.gif”);

We can add an icon for many components. Icon enhances the meaning of the component. ImageIcon is a subclass of Icon. Both belong to javax.swing package. An icon can be added to the label and if added the icon is displayed by default to the left of the label.


For the third label, lab3, all the properties are set separately in each statement. SwingConstants is an interface from javax.swing package that includes many constants with which we can set the alignment of components. The first statement sets the horizontal alignment and the second statement sets the vertical alignment of the label in the frame.


The above statement closes the JFrame window along with the application.

On JLabel, total 3 programs exist with different properties set.

1. Swing JLabel – ToolTip and Icon
2. JLabel Border and Image
3. JLabel Multiline Text

Pass your comments on this tutorial "JLabel Example ToolTip and Icon Swing".

Leave a Comment

Your email address will not be published.