JLabel in Java with Example using NetBeans

JLabel in Java:

A JLabel is a static Textview or a graphic. It is often used to describe input fields, but can also be used for headings or notes. The text can also be a hyperlink. To do this, the text must be embedded in HTML tags. Instead of text, a JLabel can also display an icon, but it can also consist of text and an icon, as in our example below.


JLabel Constructors in java

In addition to the standard constructor, the JLabel class has a few other constructors that we would like to explain in more detail in the following table:

Constructor Description
JLabel(Icon image) Here a JLabel is created with a centered icon.
JLabel(Icon image, int horizontalAlignment) This constructor creates a JLabel with an Icon whose orientation is specified by the second parameter. The constants of the SwingConstants interface, which is implemented by JLabel, can be used to specify the alignment.
JLabel(String text) Here a JLabel is created with a vertically centered text. Horizontal alignment is left-aligned by default.
JLabel(String text, Icon icon, int horizontalAlignment) This constructor can be used to set both a text and an icon for the JLabel object. The text follows the icon. The horizontal alignment can be specified via the third parameter.
JLabel(String text, int horizontalAlignment) Here a JLabel is created with a vertically centered text. The horizontal alignment is specified via the second parameter.



JLabel Set and Get Methods in Java:

In the following table we want to explain some set and get methods of the JLabel class in more detail:

Method Description
Icon getDisabledIcon() This method returns the icon to be used when the JLabel is disabled.
int getDisplayedMnemonic() Returns the hotkey set for the JLabel.
int getHorizontalAlignment() Here the horizontal icon /text alignment is returned. The integer value is the value that the constant of the SwingConstants interface has for the applicable alignment.
int getHorizontalTextPosition() This method returns the horizontal text alignment relative to the Icon.
Icon getIcon() This method returns the icon to use when the JLabel is enabled.
int getIconTextGap() The number of pixels between the icon and the text is returned here.
Component getLabelFor() Returns the component that the JLabel was set to label.
String getText() This method returns the text of the J label .
int getVerticalAlignment() Returns the vertical icon /text alignment. The integer value is the value that the constant has for the applicable alignment.
int getVerticalTextPosition() Returns the vertical text alignment relative to the icon. The integer value is the value that the constant has for the applicable alignment.
void setDisabledIcon(Icon disabledIcon) With this method, you can set an icon, which should be used if the JLabel is not active (disabled).
void setDisplayedMnemonic(char aChar) This sets the shortcut key for the JLabel. If a JLabel is associated with a component (by calling setLabelFor(Component c) ), then pressing the key with the set shortcut causes that component to request focus.
void setHorizontalAlignment(int alignment) Sets horizontal icon /text alignment. The corresponding constant can be passed as a parameter.
void setHorizontalTextPosition(int textPosition) Sets the horizontal text alignment, relative to the icon. The corresponding constant can be passed as a parameter.
void setIcon(Icon icon) This method is used to set the icon that is to be used when the JLabel is active (enabled).
void setIconTextGap(int iconTextGap) With this method, you set the number of pixels that should be between the icon and the JLabel.
void setLabelFor(Component c) This method can be used to specify which component the JLabel should be used to identify. This creates a connection between the label and the associated component.
void setText(String text) This method sets the text to be displayed.
void setVerticalAlignment(int alignment) Sets the vertical icon /text alignment. The corresponding constant can be passed as a parameter.
void setVerticalTextPosition(int textPosition) Sets the vertical text alignment, relative to the icon. The corresponding constant can be passed as a parameter.


The following is an example of a JLabel consisting of a text and an icon:

Example: JLabel with Text and image icon  in java:

Before I create our JLabel, I get an Icon from the hard drive where the image is stored. Since I don’t want to go into more detail about icons, for the sake of simplicity I’ve taken an image icon from the hard drive Feel that can be called up via the ImageIcon class. For our JLabel I use the fourth constructor from the constructor table above. As parameters, I pass the text “Programming Digest”, our icon, and the horizontal alignment, which I set to center here.  Then I set the vertical alignment to “TOP”, i.e the label will be at the top. Then I set the horizontal text position, in relation to the icon, to “LEFT”, so that the text appears to the left of the image icon, then I set the font size of the JLabel using the setFont method.

When you run the above code you will see the following  output

JLabel

Program Explanation:

first, I create the JDialog with Title text JLabel Testing Example using setTitle, and then set the window size using the setSize method 

Then, I get the image icon from the hard drive using ImageIcon Class and pass the path as a parameter in my case my image icon is stored in the F drive. You will set your image icon path where you stored the image.


then I create the JLabel and as a parameter, I pass the Text “Programming Digest “, icon object of the ImageIcon, and horizontal alignment.

Then I set the vertical alignment to “TOP”, i.e the label will be at the top.

Then I set the horizontal text position, in relation to the icon, to “LEFT”, so that the text appears to the left of the image icon

Then, I set the font size of the JLabel using the setFont method, and set its properties font style is Calibri, the Font type is BOLD and the font size is 30.



then I add the JLabel to the dialog window using the add method the JDialog class

then  i display the dialog using the setVisible method 

 

Recommended For You

About the Author: Fawad

My name is Shahzada Fawad and I am a Programmer. Currently, I am running my own YouTube channel "Expertstech", and managing this Website. My Hobbies are * Watching Movies * Music * Photography * Travelling * gaming and so on...

Leave a Reply

%d bloggers like this: