Introduction to JLabel Java Swing
JLabel is of the many Java classes from the Java Swing package. The JLabel class from the swing package is able to display a text or a picture or both. Similar to other classes in the Swing package, the label and label’s contents displayed by JLabel are aligned using horizontal and vertical alignments. The programmer can specify where the label’s contents will be displayed on the label’s display area by setting the alignments.
By default, the text or more specifically, label text is aligned vertically and is displayed at the center of their display area whereas an image or picture displayed is horizontally centered by default.
We can also easily specify the position and display the text relative to our image. The text is normally displayed at the end of our image, with the text aligned vertically, as discussed above.
It is the simplest of the Swing’s GUI components. JLabel component from the Swing package is almost the same as a label from the AWT package, the difference is JLabel does not contain user-editable text that is ‘read-only text. JLabel simply is used to display a text message or an icon/image or both on the screen but it is not able to react to events from the user for example mouse focus or keyword focus.
We can simply use JLabel by creating and using an instance for this class. Following is an example screenshot after creating an object for JLabel class and printing our label, ‘A Basic Label’.
Here we created an object of JLabel class called ‘label’ with a label text ‘A Basic Label’ given with it. You can simply write it as:
JLabel label1 = new JLabel("A basic label."); OR
label1 = new JLabel("A basic label.");
It will be displayed as:
Purpose of JLabel in Java
JLabel does not react to input events performed by the user like mouse focus or keyboard focus. It is simply a non-editable text or image or icon or both. JLabel is generally used along with those components that do not have their own ability to explain or demonstrate their purpose. The JLabel object created will provide our user, the text instructions or information on our GUI.
For example, a text area for entering a Name or Password, etc will require a label to tell the user about the text box.
Find this example explained below with screenshots.
Without the usage of JLabel, the text boxes will appear lost to a user since they do not tell themselves what the user is expected to enter in the text field. Take the following example, we have added a text field without any labels.
Note, you can simply add a text field using a following simple line of code.
JTextField textEmail = new JTextField(20); //creating object for text field
textEmail.setBounds(50,50, 150,20); //setting the bounds for the text box
But if this text field is used in combination with JLabel, it will appear as below and will make more sense, isn’t it?
Below is another example where we used our previous text field along with which we have added a simple one-line string ‘Enter Email address’, suggesting our user that he needs to add his/her email address in the given text area.
As shown above, we can simply add a text field. Now we will add a label too as shown below:
textLabel = new JLabel("Enter e-mail address:");
JTextField textEmail = new JTextField(20);
This was a simple example we created. It was a simple program displaying a text field and a label with it. We also can add an icon along with using another commonly used method with JLabel, known as the setIconTextGap method. This method helps the programmer to specify how many pixels our text and our image should be displayed apart.
Constructors of JLabel
Java JLabel class has several constructors that can be used to create our label with different characteristics.
- JLabel (): This constructor creates an empty label that is without any text. This instance of the class creates the label with no image and an empty string or text for its title. The text can be set at a later time.
- JLabel (Icon Image): This constructor creates a label with only a specified icon or image. The icon or image file can be used from your own file system.
- JLabel (String Text): This instance creates a label with a specific text while declaring our constructor. Apart from the above mentioned basic constructors, we also have the following that can use.
- JLabel (Icon Image, int horizontalAlignment): This constructor instance is used to create a specified image or icon along with horizontal alignment.
- JLabel(String text, int horizontalAlignment): This constructor instance is used to create a specified text along with horizontal alignment.
- JLabel (String text, Icon icon, int horizontalAlignment): This constructor instance is used to create a specified image or icon, text as well as its alignment as ‘horizontal’.
Examples of JLabel
Following is an example for creating a simple program of ‘Sign In Form’ with two labels added for two text fields displaying their nature. We also added a button with its own label displaying text as ’Sign In’.
public static void main(String args)
//Adding our Frame
JFrame f= new JFrame("Label Demo");
//Creating objects for our Labels
//Creating object for Sign In button
//Creating object for our text boxes
//Creating our button
Button1=new JButton("Sign In");
//Creating our first Label
label1=new JLabel("User Name:");
//Creating our second label
//Creating our first text field
TextBox1 = new JTextField(20);
//Creating our second text field
TextBox2 = new JTextField(20);
//Setting bound for our Label1
//Setting bound for our Label2
//Setting bound for our TextBox1
//Setting bound for our TextBox2
//Setting bound for our Button1
//Adding our Label1,Label2,TextBox1,TextBox2,Button1 to our frame
You can see the code below, I have used Eclipse for writing the code.
When the above lines of code are executed, we get the following window as our output. Check it out:
Common Methods Used in JLabel
We have already discussed JLabel and how to create one as a text or an icon. Following is another list of methods that are generally used along with JLabel in our programs. These are the commonly used methods of JLabel class.
- getIcon (): This method is used to get the image displayed that our label had displayed.
- setIcon(Icon i): This method helps set our icon to be displayed to our image, i.
- getText(): This method returns our text which is displayed by our label.
- setText(String s): This method simply sets the text that will be displayed by our label to our string, s.
Above are a few methods used generally along with JLabel among others like setDisplayedMnemonic, getDisplayedMnemonic, etc.
JLabel is a descendant from JComponent that is used to create a simple text or icon labels. They are used to provide text instructions and other information, if required, on the graphical interface for our users to make their experience easy.
We use the JLabel component from the Swing package when we need one graphical interface component that needs a message or an image to be displayed.
This is a guide to JLabel in Java. here we discuss the Purpose, Constructors, Examples and Common Methods Used in JLabel. You may also look at the following article to learn more –