In this article JLable in Swing we give the information about it is used to display text, images, or both in the user interface. JLabel does not take input from the user but only displays information.

JLable in Swing:-

JLabel is an important component in Swing that is used to display text, images, or both in the user interface. JLabel does not take input from the user but only displays information.

Features of JLabel:

  1. Displaying text:

In JLabel you can display a static text, which gives information to the user.

  1. Displaying Image:

You can also display an image using JLabel.

  1. Text and Image Together:

In JLabel you can display both text and image together.

  1. No Auto-Line Breaking:

In JLabel the text does not automatically go to the next line. If the text is long, it has to be scrolled or some other technique is used.

Constructor of JLabel:

  1. JLabel():

This creates an empty label, which contains neither text nor image.

  1. JLabel(String text):

This creates a label that contains a text.

  1. JLabel(Icon icon):

This creates a label that contains an image (Icon).

  1. JLabel(String text, Icon icon, int alignment):

This creates a label that contains both text and image, and you can also set the position of the text.

//Example of JLabel

import javax.swing.*;

public class JLabelExample

{

            public static void main(String[] args)

            {

            JFrame frame = new JFrame(“JLabel Example”);

            // JLabel with text

            JLabel label1 = new JLabel(“Wel-Come to Swing”);

                // JLabel with image

            Icon icon = new ImageIcon(“path/to/image.jpg”);  // Provied to Image path

            JLabel label2 = new JLabel(icon);

                // JLabel with both text and image

            JLabel label3 = new JLabel(“Image or Text”, icon, JLabel.CENTER);

                frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));

                frame.add(label1);

            frame.add(label2);

            frame.add(label3);

                frame.setSize(300, 300);

            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            frame.setVisible(true);

    }

}

In this example:

  1. The first JLabel shows only text.
  2. The second JLabel shows only an image.
  3. The third JLabel shows both text and an image.

Methods of JLabel:

  1. setText(String text):

Sets the text to be displayed in the JLabel.

  1. setIcon(Icon icon):

Sets the image (icon) to be displayed in the JLabel.

  1. setHorizontalAlignment(int alignment):

Sets the horizontal alignment of the text or image in the JLabel.

Example: JLabel.CENTER or JLabel.LEFT

  1. setVerticalAlignment(int alignment):

Sets the vertical alignment of the text or image in a JLabel.

  1. getText():

Gets the text set in a JLabel.

  1. getIcon():

Gets the image set in a JLabel.

Summary:

JLabel is used to display text and images in Swing.

It is a static component, i.e. no input is taken from the user.

Through JLabel, you can show important information, messages, or images to the user.

JLabel is very easy to use and is essential to make the user interface of Swing attractive.

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *