Getting Started With Javax Swing JLabel Tutorial For Beginners

Hello ,welcome to an another and interesting article about Javax Swing JLabel.In my last post we learnt about how to use JFrame in Java and before moving ahead to this tutorial  it’s good to have the basic knowledge of JFrame class because i will be using some methods and classes in this tutorial which i have already  taught you in the previous tutorial .So if you did not see my last post go through my previous post .Link is given below..

JFrame Java Tutorial

 After going through this tutorial you will get to know about JLabel.

So let’s start our tutorial Javax Swing JLabel.

Javax Swing JLabel

what is JLabel  ?

  • It is a component of Swing that is used for creating labels and it displays infromation.
  • It is the simplest component of Swing because it is passive which means it does not respond to user’s input and it just simply display information(output).
  • The information can consists of text, icon/image or can be the combination of both.

So this was all about JLabel in java Swing and now we will learn how to create a  JLabel .

Creating a JLabel

Steps for creating a JLabel

  • Create an object of the JLabel class and pass any text (within quotes) in its constructor. Example- JLabel label=new JLabel(“myLabel”) ;
  • We can also use a method setText() for setting  the  text of label. We have to just call the method using the object of the JLabel class and have to pass the text in its argument (within quotes) . Example- label.setText(“myLabel”);
  • Programming code is given below :
  • The interesting thing about this program is that although we have created the JLabel ,nothing shows up in the output just because we have not added the label to the content pane of the frame.
  • To add it to the frame’s content pane we have to call add() method using the reference of JFrame in which we have to pass the object of JLabel as an argument .
  • Programming code is given below :
  • Output :
javax swing JLabel
javax swing JLabel -fig-1
  • As we can see in the above image  by default  add() method added the label to the center location of the content pane .
  • When a component is added to the center ,its size is adjusted automatically to fit the size of the center.
  • If we want to add the label in another location of the content pane then first we have to set the layout of the content pane as null because by default it uses another layout and we can set the location and size of the label using the setBounds() method .
  • Programming code is given below :
  • Output  :
setting size and location of JLabel
Javax Swing JLabel-fig-2
  • As we can see in the above image the size and location of the label has changed.

Setting the Font style and Font size of a JLabel Text

steps

  • First of all we need to create the object of Font class in which we have to pass 3 values in its constructor
    • First is Font name within quotes (You can find font names in many editor ).
    • Second is Font variant  in which we pass a constant of the Font class  (Example :  Font.BOLD).
    • Third is Font size.
  • Now we will apply it on the JLabel using the method setFont() and will be  calling it through the object of JLabel   class .
  • Programming code is given below :
  • Output  :
Setting Font style and Font size of JLabel text
Javax Swing JLabel-fig-3
  • As we can see in the above image Font style and Font size of the JLabel text has changed .

Setting Image In JLabel

Steps

  • For setting Image in JLabel we need to create the object of ImageIcon class and  in its  constructor we have to pass the full path of the image/icon  with its correct extension .
  • If the image is in the same folder we have created the program then we have to give only image name and its extension otherwise if image is in the other folder then  we have to give the whole path of the image .
  • After that we have to pass the object of the ImageIcon class in the constructor of JLabel class as an argument.
  • Then we can set the location and size of the Image/icon using the method setBounds() .
  • Intersting thing is that if we dont know the actual width and height of the image and its not perfectly fitting in label then we can use the two methods of icon getIconWidth() and getIconHeight() as  arguments in setBounds() method .
  • The next thing we have to do is to add the label to the content pane of the frame .
  • Programming code is given below :
  • Output  :
Setting image in JLabel
Javax swing JLabel-fig-4
  • As we can see in above output we have  perfectly set the image in JLabel .

Setting Both Image And Text In JLabel

Steps

  • First of all we have to create the  object of ImageIcon class and have to pass the image name and its extension if it is in the same directory in which we are running our application otherwise we have to give the whole path of the image file .
  • The next thing we have to do is to create the object of JLabel class and have to pass 3 values in its constructor.
    • first argument is the text(within quotes) that we want to write in the label.
    • second argument is the object of the ImageIcon class .
    • third argument is  the alignment of the label which means where the label will be aligned in the given width . Example(JLabel.LEFT,JLabel.RIGHT,JLabel.CENTER).
  • We will see  all the three alignments in our programming example.
  • The next thing we need to do is to add the label into the content pane of the frame.
  • Programming code is given below :
  • Output  :
Setting both image and text in JLabel
Javax Swing JLabel-fig-5
  • As we can see in the above image we have set both image and text in JLabel with LEFT,CENTER and RIGHT alignments.

Thats all for the tutorial of Javax Swing JLabel . Thank You.

1 thought on “Getting Started With Javax Swing JLabel Tutorial For Beginners”

Leave a Comment