Perhaps JLabelis the simplest Swing’s GUI component which simply renders a text message or an icon or both on screen. This article presents common practices when using JLabelin Swing development.

Table of content:

    1. Creating a JLabel object
    2. Adding the label to a container
    3. Customizing JLabel’s appearance
    4. Labeling a component
    5. JLabel demo program
 

1. Creating a JLabel object

  • Create a basic label with some text:
    JLabel label = new JLabel("This is a basic label");

    Image: basic label 

  • Create a label with empty text and set the text later:
    JLabel label = new JLabel();
    label.setText("This is a basic label");
  • Create a label with only an icon (the icon file is in the file system and relative to the program):
    JLabel label = new JLabel(new ImageIcon("images/attention.jpg"));

    Image: icon only label

    This is the common way to display an image/icon in Swing.

  • Create a label with only an icon (the icon file is in the classpath or in a jar file):
    String iconPath = "/net/codejava/swing/jlabel/Color.png";
    Icon icon = new ImageIcon(getClass().getResource(iconPath));
    JLabel label = new JLabel(icon);
  • Create a label with both text and icon and horizontal alignment is center:
    JLabel label = new JLabel("A label with icon and text",
    		new ImageIcon("images/attention.jpg"),
    		SwingConstants.CENTER);

    Image: icon and text label
     

    In this case, we can set the gap between the icon and the text as follows:

    label.setIconTextGap(10);
     



    That will set 10 pixels gap between the icon and the text.

  • We can also specify horizontal alignment of the text/icon when creating the label:
JLabel label = new JLabel("Enter first name:", SwingConstants.RIGHT);

JLabel label = new JLabel(new ImageIcon("images/attention.jpg"),
							SwingConstants.LEFT);
 

2. Adding the label to a container

  • A JLabel is usually added to a JPanel, a JFrame, a JDialog or a JApplet:
    frame.add(label);
    dialog.add(label);
    panel.add(label);
    applet.getContentPane().add(label);
     

  • Adding a JLabel to a container with a specific layout manager:
frame.add(label, BorderLayout.CENTER);
panel.add(label, gridbagConstraints);
 

3. Customizing JLabel’s appearance

  • Change font style, background color and foreground color of the label:
    label.setFont(new java.awt.Font("Arial", Font.ITALIC, 16));
    label.setOpaque(true);
    label.setBackground(Color.WHITE);
    label.setForeground(Color.BLUE);

    Image: customized label 1 

    NOTE:by default, the label’s background is transparent, so if you want to set background, you have to set the label’s opaque property to true.

  • Instead of using the methods above, we can use HTML code to customize the label’s appearance. For example:
    label.setText("<html><font color=red size=4><b>WARNING!</b></html>");

    Image: html label 

  • We also must you HTML code if we want to add line breaks in the label’s text. For example:
JLabel label = new JLabel("<html><i>This label has <br>two lines</i><html>");

Image: label text has line breaks

 

4. Labeling a component

A JLabelis usually used for labeling a component such as a JTextField. If we want to allow the users accessing a text field using shortcut key (mnemonic), use the following code:

JTextField textEmail = new JTextField(20);

JLabel label = new JLabel("Enter e-mail address:");
label.setLabelFor(textEmail);
label.setDisplayedMnemonic('E');
 

Image: labeling a text field

You can notice that the ‘E’ letter in the label in underlined, so the users can type Alt + E to get focus on the text field.

 

5. JLabel demo program

For reference, we created a Swing program that demonstrates various techniques mentioned when working with JLabelcomponent. The program looks like this:

JLabel demo program

You can download the program’s source code in the attachment section.

 

Other Java Swing Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Attachments:
Download this file (SwingJLabelDemo.zip)SwingJLabelDemo.zip[Demo program for JLabel]12 kB

Add comment

   


Comments 

#12nawawih2022-08-01 01:47
i am tryl for printting barcode to printer bixolon
Quote
#11ano2021-05-24 03:16
good job codejava. you are good people
Quote
#10Lakwin2021-02-26 02:08
These tutorials are very useful
Quote
#9Nam2020-07-21 18:05
Hi Fongho David,
I'm very fine. So I recommend you to follow my tutorials for beginners here: www.codejava.net/java-core-tutorials
Quote
#8FONGHO DAVID2020-07-21 12:21
Hello Nam, hope you are doing great? I really wish to learn programming and will like to start with Java.
Quote