1. Technology

Creating Labels Example Program


Java Code:

This example Java program goes through the different constructors that can be used to create JLabels. When run a JFrame appears showing the different JLabels.

For usage tips on using the JLabel class have a look at the JLabel Overview.

//Imports are listed in full to show what's being used
//could just import javax.swing.* and java.awt.* etc..
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import javax.swing.UIManager;
import javax.swing.Icon;

public class SwingLabelsExample {
    //Note: Typically the main method will be in a
    //separate class. As this is a simple one class
    //example it's all in the one class.
    public static void main(String[] args) {
         //Use the event dispatch thread for Swing components
         EventQueue.invokeLater(new Runnable()
             public void run()
                 new SwingLabelsExample();         
    public SwingLabelsExample()
        JFrame guiFrame = new JFrame();
        //make sure the program exits when the frame closes
        guiFrame.setTitle("BorderLayout Example");
        //This will center the JFrame in the middle of the screen
        JPanel labelPanel = new JPanel();
        //Make a Grid that has three rows and four columns
        labelPanel.setLayout(new GridLayout(4,2));   
        guiFrame.add(labelPanel, BorderLayout.CENTER);
        //creating a border to highlight the label areas
        Border outline = BorderFactory.createLineBorder(Color.black);
        //Using the UIManager class to get default Java icon so that 
        //the example code works. You can use the Icon class to define
        //a specify image to use.
        //Normally these icons are used by a JOptionPane for different 
        //types of dialog boxes.
        Icon infoIcon = UIManager.getIcon("OptionPane.informationIcon");
        Icon errorIcon = UIManager.getIcon("OptionPane.errorIcon");
        Icon warningIcon = UIManager.getIcon("OptionPane.warningIcon");
        Icon questionIcon = UIManager.getIcon("OptionPane.questionIcon");
        //Create a label with an image
        JLabel iconLabel = new JLabel(infoIcon);
        //Create a label with an image, text and centered horizontally
        JLabel imageTextLabel = new JLabel("Some Text", warningIcon, JLabel.CENTER);
        //Create a label with text and right alignment
        JLabel textAlignLabel = new JLabel("More text", JLabel.RIGHT);
        //Create a label with an image and leading alignemnt
        JLabel iconAlignLabel = new JLabel(errorIcon, JLabel.LEADING);
        //Set the text after the creation of the label
        JLabel setTextLaterLbl = new JLabel();
        setTextLaterLbl.setText("Here's some text");
        //Set the icon after the creation of the label
        JLabel setIconLaterLbl = new JLabel();
        //Find out the text and Icon values of a label
        String lblText = setTextLaterLbl.getText();
        Icon lblIcon = setIconLaterLbl.getIcon();
        //HTML formatting can be used in the String
        JLabel htmlStringLabel = new JLabel("<html><b>Line One</b><br><i>Line Two</i><br>Line Three</html>");
See More About
  1. About.com
  2. Technology
  3. Java
  4. Java Syntax
  5. Utilizing the Java API
  6. javax.swing
  7. Creating Labels Example Program

©2014 About.com. All rights reserved.