JLabel Example ToolTip and Icon Swing

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.

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 Reply

Your email address will not be published. Required fields are marked *