Java Swing JLabel
Java Swing JLabel
A JLabel class object is a component for placing text,an image,or both in a container.It is used to display a single line of read only text. The text of JLabel cannot be edit by the user directly,it can be changed by an application. It inherits JComponent class.
Label's contents are aligned by setting the vertical and horizontal alignment in its display area. By default, labels are vertically centered in their display area.
Constructors used with JLabel
Type of Constructor | Constructor Description |
---|---|
JLabel() | It Creates a JLabel instance with no image and with an empty string for the title. |
JLabel(String str) | It Creates a JLabel instance with the specified text. |
JLabel(Icon ic) | It Creates a JLabel instance with the specified image. |
JLabel(String s, Icon i, int horizontalAlignment) | It Creates a JLabel instance with the specified text, image and horizontal alignment. |
JLabel(Icon image, int horizontalAlignment) | ItCreates a JLabel instance with the specified image and horizontal alignment. |
JLabel(String text, int horizontalAlignment) | It Creates a JLabel instance with the specified text and horizontal alignment. |
The following example creates a label with Welcome as the the text on the label.
Example
Basic Methods of JLabel class :
Method Name | Description |
---|---|
String getText() | This method returns the text of the label. |
void setText(String str) | This method is used to set specified text on label |
void setIcon(Icon ic) | This method is used to set the specific Icon on the label. |
Icon getIcon() | This method is used to get the Icon of the label. |
void setEnabled(boolean bl) | This method is used to enable or disable the label. |
void setHorizontalAlignment(int alignment) | This method sets the alignment of the label's contents along the X axis. |
int getHorizontalAlignment() | This method returns the alignment of the label's contents along the X axis. |
void setBounds(x, y, width, height) | This method is used to set x,y cordinates and height,width of the label. |
Example of JLabel with JLabel(String) and JLabel(Icon) Constructors
import javax.swing.*;
import java.awt.*;
class LabelEx extends JFrame
{
JLabel lbl1,lbl2;
Icon ic;
public LabelEx() {
lbl1=new JLabel("Welcome");
lbl1.setEnabled(true);
lbl1.setForeground(Color.BLUE);
Font f=new Font("Comic Sans MS", Font.BOLD, 26);
lbl1.setFont(f);
lbl1.setBounds(20,20,200,50);
add(lbl1);
ic= new ImageIcon("D:\\compuhelp-website\\img\\COMPUHELP.PNG");
lbl2=new JLabel(ic);
lbl2.setBounds(20, 80, 200, 50);
add(lbl2);
setLayout(null);
setSize(250,200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class LabelExample1
{
public static void main(String[] args)
{
LabelEx btn=new LabelEx();
}
}
Output:
The code above will display :
Example of JLabel with
JLabel(String text, int horizontalAlignment),
JLabel(String s,Icon i,int horizontalAlignment) and
setToolTipText()
import javax.swing.*;
import java.awt.*;
class LabelEx extends JFrame
{
JLabel lbl1,lbl2,lbl3;
Icon ic;
public LabelEx() {
ic=new ImageIcon("D:\\shelza\\compuhelp-website\\java-videos\\img\\COMPUHELP.PNG");
String label = "<html>" + "This is a" + "<br>" + "swing label" + "</html>";
lbl1=new JLabel(label,JLabel.RIGHT);
lbl1.setEnabled(true);
lbl1.setForeground(Color.BLUE);
lbl1.setBounds(20,20,200,50);
add(lbl1);
lbl2=new JLabel("label2 with tooltip text");
lbl2.setToolTipText("Learn programming in Compuhelp");
lbl2.setBounds(20, 80, 200, 50);
add(lbl2);
lbl3=new JLabel("Compuhelp", ic, JLabel.CENTER);
lbl3.setBounds(20, 140, 300, 50);
add(lbl3);
setLayout(null);
setSize(350,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class LabelExample1
{
public static void main(String[] args)
{
LabelEx lbex=new LabelEx();
}
}
Output:
The code above will display :
Example of Java Swing JButton with ActionListener
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class SwingLabel extends JFrame implements ActionListener
{
JButton btn1;
JButton btn2;
JLabel lbl3 ;
JLabel lbl1,lbl2;
Font f;
public SwingLabel()
{
lbl1=new JLabel("Change color");
lbl2=new JLabel("Change font");
lbl3=new JLabel("Click on Both Buttons");
btn1=new JButton("Color");
btn2=new JButton("Font");
lbl1.setBounds(30, 40, 100, 50);
lbl1.setOpaque(true);
lbl2.setBounds(180, 40, 200, 50);
lbl3.setBounds(30, 10, 200, 30);
btn1.setBounds(30, 100, 100, 30);
btn2.setBounds(160, 100, 100, 30);
add(lbl1);
add(lbl2);
add(lbl3);
add(btn1);
add(btn2);
btn1.addActionListener(this);
btn2.addActionListener(this);
setSize(400,300);
setLayout(null);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==btn1)
{
lbl1.setBackground(Color.cyan);
lbl1.setForeground(Color.red);
}
if(ae.getSource()==btn2)
{
f=new Font("Jokerman", Font.BOLD, 20);
lbl2.setFont(f);
}
}
}
public class LabelExample
{
public static void main(String[] args)
{
SwingLabel label=new SwingLabel();
}
}
Output:
The code above will display :