1. Computing

Creating Labels Example Program

By

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()
         {
             
            @Override
             public void run()
             {
                 
                 new SwingLabelsExample();         
             }
         });
              
    }
    
    public SwingLabelsExample()
    {
        JFrame guiFrame = new JFrame();
        
        //make sure the program exits when the frame closes
        guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        guiFrame.setTitle("BorderLayout Example");
        guiFrame.setSize(700,300);
      
        //This will center the JFrame in the middle of the screen
        guiFrame.setLocationRelativeTo(null);
        
        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);
        iconLabel.setBorder(outline);
        
        //Create a label with an image, text and centered horizontally
        JLabel imageTextLabel = new JLabel("Some Text", warningIcon, JLabel.CENTER);
        imageTextLabel.setBorder(outline);
        
        //Create a label with text and right alignment
        JLabel textAlignLabel = new JLabel("More text", JLabel.RIGHT);
        textAlignLabel.setBorder(outline);
        
        //Create a label with an image and leading alignemnt
        JLabel iconAlignLabel = new JLabel(errorIcon, JLabel.LEADING);
        iconAlignLabel.setBorder(outline);
        
        //Set the text after the creation of the label
        JLabel setTextLaterLbl = new JLabel();
        setTextLaterLbl.setBorder(outline);
        setTextLaterLbl.setText("Here's some text");
        
        //Set the icon after the creation of the label
        JLabel setIconLaterLbl = new JLabel();
        setIconLaterLbl.setBorder(outline);
        setIconLaterLbl.setIcon(questionIcon);
        
        //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>");
           
              
        labelPanel.add(iconLabel);
        labelPanel.add(imageTextLabel);
        labelPanel.add(textAlignLabel);
        labelPanel.add(iconAlignLabel);
        labelPanel.add(setIconLaterLbl);
        labelPanel.add(setTextLaterLbl);
        labelPanel.add(htmlStringLabel);
        guiFrame.setVisible(true);
    }
}
See More About
  1. About.com
  2. Computing
  3. Java
  4. Java Syntax
  5. Utilizing the Java API
  6. javax.swing
  7. Creating Labels Example Program

©2014 About.com. All rights reserved.