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..
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 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import javax.swing.*; import java.awt.*; public class myLabel { public static void main(String args[] ) { JFrame f= new JFrame(); f.setTitle("JLabel"); f.setVisible(true); f.setSize(500,500); Container c= f.getContentPane(); JLabel label=new JLabel("myLabel");//creating JLabel //label.setText("myLabel"); // we can also use this setText() method for setting text of JLabel f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } |
- 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 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import javax.swing.*; import java.awt.*; public class myLabel { public static void main(String args[] ) { JFrame f= new JFrame(); f.setTitle("JLabel"); f.setVisible(true); f.setSize(500,500); Container c= f.getContentPane(); JLabel label=new JLabel("myLabel"); f.add(label);//adding label to content pane f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } |
- Output :
- 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 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import javax.swing.*; import java.awt.*; public class myLabel { public static void main(String args[] ) { JFrame f= new JFrame(); f.setTitle("JLabel"); f.setVisible(true); f.setSize(500,500); Container c= f.getContentPane(); c.setLayout(null);//setting Layout as null JLabel label=new JLabel("myLabel"); f.add(label); label.setBounds(100,50,100,100);//setting size and location of label f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } |
- Output :
- 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 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import javax.swing.*; import java.awt.*; public class myLabel { public static void main(String args[] ) { JFrame f= new JFrame(); f.setTitle("JLabel"); f.setVisible(true); f.setSize(500,500); Contaner c= f.getContentPane(); c.setLayout(null); JLabel label=new JLabel("myLabel"); f.add(label); label.setBounds(100,50,100,100); Font font=new Font("ARIAL",Font.ITALIC,20);//creating object of Font class label.setFont(font);//calling setFont() method f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } |
- Output :
- 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 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import javax.swing.*; import java.awt.*; public class myLabel { public static void main(String args[] ) { JFrame f= new JFrame(); f.setTitle("JLabel"); f.setVisible(true); f.setSize(500,500); Container c= f.getContentPane(); c.setLayout(null); //creating object of ImageIcon class ImageIcon icon =new ImageIcon("Java.png"); JLabel label=new JLabel(icon); //setting size and location of JLabel image label.setBounds(100,50,icon.getIconWidth(),icon.getIconHeight()); // adding label to the content pane of the frame f.add(label); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } |
- Output :
- 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 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import javax.swing.*; import java.awt.*; public class myLabel { public static void main(String args[] ) { JFrame f= new JFrame(); f.setTitle("JLabel"); f.setVisible(true); f.setSize(500,500); Container c= f.getContentPane(); c.setLayout(null); //creating the object of ImageIcon class ImageIcon icon =new ImageIcon("Java.png"); //creating first JLabel class and passing left alignment JLabel label=new JLabel("Java",icon,JLabel.LEFT); label.setBounds(25,50,600,400); //creating second JLabel class and passing right alignment JLabel label2=new JLabel("Java",icon,JLabel.RIGHT); label2.setBounds(25,50,600,400); //creating third JLabel class and passing center alignment JLabel label3=new JLabel("Java",icon,JLabel.CENTER); label3.setBounds(25,50,600,400); //adding labels to to the content pane of the frame f.add(label); f.add(label2); f.add(label3); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } |
- Output :
- 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.
how I should change the font color?