JLabel | Java Swing Last Updated : 15 Apr, 2021 Comments Improve Suggest changes Like Article Like Report JLabel is a class of java Swing . JLabel is used to display a short string or an image icon. JLabel can display text, image or both . JLabel is only a display of text or image and it cannot get focus . JLabel is inactive to input events such a mouse focus or keyboard focus. By default labels are vertically centered but the user can change the alignment of label.Constructor of the class are : JLabel() : creates a blank label with no text or image in it.JLabel(String s) : creates a new label with the string specified.JLabel(Icon i) : creates a new label with a image on it.JLabel(String s, Icon i, int align) : creates a new label with a string, an image and a specified horizontal alignment Commonly used methods of the class are : getIcon() : returns the image that the label displayssetIcon(Icon i) : sets the icon that the label will display to image igetText() : returns the text that the label will displaysetText(String s) : sets the text that the label will display to string s 1. Program to create a blank label and add text to it. Java // Java Program to create a // blank label and add text to it. import java.awt.event.*; import java.awt.*; import javax.swing.*; class text extends JFrame { // frame static JFrame f; // label to display text static JLabel l; // default constructor text() { } // main class public static void main(String[] args) { // create a new frame to store text field and button f = new JFrame("label"); // create a label to display text l = new JLabel(); // add text to label l.setText("label text"); // create a panel JPanel p = new JPanel(); // add label to panel p.add(l); // add panel to frame f.add(p); // set the size of frame f.setSize(300, 300); f.show(); } } Output : 2. Program to create a new label using constructor - JLabel(String s) Java // Java Program to create a new label // using constructor - JLabel(String s) import java.awt.event.*; import java.awt.*; import javax.swing.*; class text extends JFrame { // frame static JFrame f; // label to display text static JLabel l; // default constructor text() { } // main class public static void main(String[] args) { // create a new frame to store text field and button f = new JFrame("label"); // create a label to display text l = new JLabel("new text "); // create a panel JPanel p = new JPanel(); // add label to panel p.add(l); // add panel to frame f.add(p); // set the size of frame f.setSize(300, 300); f.show(); } } Output : 3. Program to create a label and add image to it . Java // Java Program to create a label // and add image to it . import java.awt.event.*; import java.awt.*; import javax.swing.*; class text extends JFrame { // frame static JFrame f; // label to display text static JLabel l; // default constructor text() { } // main class public static void main(String[] args) { // create a new frame to store text field and button f = new JFrame("label"); // create a new image icon ImageIcon i = new ImageIcon("f:/image.png"); // create a label to display image l = new JLabel(i); // create a panel JPanel p = new JPanel(); // add label to panel p.add(l); // add panel to frame f.add(p); // set the size of frame f.setSize(500, 500); f.show(); } } Output : 4. Program to add a image and string to a label Java // Java Program to add a image and string // to a label with horizontal alignment import java.awt.event.*; import java.awt.*; import javax.swing.*; class text extends JFrame { // frame static JFrame f; // label to display text static JLabel l; // default constructor text() { } // main class public static void main(String[] args) { // create a new frame to store text field and button f = new JFrame("label"); // create a new image icon ImageIcon i = new ImageIcon("f:/image.png"); // create a label to display text and image l = new JLabel("new image text ", i, SwingConstants.HORIZONTAL); // create a panel JPanel p = new JPanel(); // add label to panel p.add(l); // add panel to frame f.add(p); // set the size of frame f.setSize(600, 500); f.show(); } } Output : Note : This programs might not run in an online compiler please use an offline IDE. Comment More infoAdvertise with us Next Article JLabel | Java Swing andrew1234 Follow Improve Article Tags : Java Programming Language java-swing Practice Tags : Java Similar Reads Java Swing | JTable The JTable class is a part of Java Swing Package and is generally used to display or edit two-dimensional data that is having both rows and columns. It is similar to a spreadsheet. This arranges data in a tabular form.Constructors in JTable: JTable(): A table is created with empty cells.JTable(int r 2 min read Java Swing | JSlider JSlider is a part of Java Swing package . JSlider is an implementation of slider. The Component allows the user to select a value by sliding the knob within the bounded value . The slider can show Major Tick marks and also the minor tick marks between two major tick marks, The knob can be positioned 6 min read Java Swing | JToolBar JToolBar is a part of Java Swing package. JToolBar is an implementation of toolbar. The JToolBar is a group of commonly used components such as buttons or drop down menu. JToolBar can be dragged to different locations by the user Constructors of the class are: JToolBar() : creates a new toolbar with 6 min read JRadioButton | Java Swing We use the JRadioButton class to create a radio button. Radio button is use to select one option from multiple options. It is used in filling forms, online objective papers and quiz. We add radio buttons in a ButtonGroup so that we can select only one radio button at a time. We use "ButtonGroup" cla 6 min read Java AWT Label Abstract Window Toolkit (AWT) was Java's first GUI framework, and it has been part of Java since version 1.0. It contains numerous classes and methods that allow you to create windows and simple controls. Controls are components that allow a user to interact with your application in various ways. Th 7 min read Java Swing | JProgressBar JProgressBar is a part of Java Swing package. JProgressBar visually displays the progress of some specified task. JProgressBar shows the percentage of completion of specified task.The progress bar fills up as the task reaches it completion. In addition to show the percentage of completion of task, i 4 min read Java Swing - JPanel With Examples JPanel, a part of the Java Swing package, is a container that can store a group of components. The main task of JPanel is to organize components, various layouts can be set in JPanel which provide better organization of components, however, it does not have a title bar. Constructors of JPanel JPanel 4 min read Java JTabbedPane JTabbedPane is a GUI(Graphical User Interface) component in the Java Swing library that allows you to create a tabbed pane interface. A tabbed pane is a container that can store and organize multiple components into distinct tabs. It contains a collection of tabs. When you click on a tab, only data 5 min read Java Swing | ScrollPaneLayout Class The layout manager used by JScrollPane. JScrollPaneLayout is based on nine components: a viewport, two scrollbars, a row header, a column header, and four "corner" components. Constructor of the class: ScrollPaneLayout(): It is used to Construct a new ScrollPaneLayout. Commonly Used Methods: remov 4 min read Java Swing | Look and Feel Swing is a GUI Widget Toolkit for Java. It is an API for providing a Graphical User Interface to Java Programs. Unlike AWT, the Swing toolkit provides more advanced features such as animations and event handling.Initially, there were very few options for colors and other settings in Java Swing, whic 5 min read Like