Java JLabel

The object of JLabel class is a component for placing text in a container. It is used to display a single line of read only text. The text can be changed by an application but a user cannot edit it directly. It inherits JComponent class.

JLabel class declaration

Let's see the declaration for javax.swing.JLabel class.

public class JLabel extends JComponent implements SwingConstants, Accessible

Commonly used Constructors:

  • JLabel(String s, Icon i, int horizontalAlignment)
  • Creates a JLabel instance with the specified text, image, and horizontal alignment.
  • Constructor Constructor
    JLabel() Creates a JLabel instance with no image and with an empty string for the title.
    JLabel(String s) Creates a JLabel instance with the specified text.
    JLabel(Icon i) Creates a JLabel instance with the specified image.

    Commonly used Methods:

  • Icon getIcon()
  • It returns the graphic image that the label displays.
  • int getHorizontalAlignment()
  • It returns the alignment of the label's contents along the X axis.
  • Methods Description
    String getText() t returns the text string that a label displays.
    void setText(String text) It defines the single line of text this component will display.
    void setHorizontalAlignment(int alignment) It sets the alignment of the label's contents along the X axis.

    Java JLabel Example

    Flash Soon


    Output

    Java JLabel Example with ActionListener

    Flash Soon


    Output


    Advertisement

    © Sapster 2017