Set look and feel in JSwing

Look and feel is a part of the Java Swing widget toolkit used to change the look of the graphical user interface at runtime. Although JSwing default look and feel is SystemLookAndFeel, you can also change it on start-up. In this tutorial, we will tell you how to change the look and feel of your application.

You will learn how to:

  • Programming to initialize items of JComboBox.
  • Create a JComboBox event handler - itemStateChanged

 

Prerequisites

Before you begin to read this tutorial, you must:

  1. Familiar with Java swing development, go to oracle Java Swing to get tutorials reference.
  2. Read the first three tutorials on JSwing.
  3. Familiar with the operation of Apache NetBeans.
  4. Understanding what LookAndFeel is and its effects.

 

Create a SetLookAndFeel Form

  1. Create a JFrame class named SetLookAndFeel on the form of the Packages, and then add three JButtons and one JLabel, JComboBox, and JTextArea to the JFrame, the tree diagram as shown in the following figure.

    SetLookAndFeel tree view

  2. Change the properties of the component as shown in the table below.
    ComponentstextactionCommandname
    jLbael1Message Message
    jComboBox1 comboBoxcomboBox
    jTextArea1TextArea TextArea
    jButton1ConfirmConfirmConfirm
    jButton2ExitExitExit
    jButton3TipsTipsTips
  3. After the successful step above, Press Ctrl-S to save the file, and press F9 to Compile the Form, shown as the following.

    SetLookAndFeel Form

Create a SetLookAndFeel App

Create a Java class named SetLookAndFeel on the app of the Packages, and then add code to the Class.

package app;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.UnsupportedLookAndFeelException;
import jswing.application.*;

public final class SetLookAndFeel extends App {

    public static void main(String args[]) {

        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Form.open("form.SetLookAndFeel","SetLookAndFeel");
                UserInterface.initialize("app.SetLookAndFeel",Form.getCurrentForm());
                
                //JComboBox initialization---start
                javax.swing.UIManager.LookAndFeelInfo[] lookAndFeels = javax.swing.UIManager.getInstalledLookAndFeels();
                for (javax.swing.UIManager.LookAndFeelInfo s : lookAndFeels) {
                    ((javax.swing.JComboBox)Form.getCurrentForm().getComponentByName("comboBox")).addItem(s.getClassName());
                }
                //JComboBox initialization---end
            }
        });
    }
    @Override
    public void itemStateChanged(java.awt.Component component) {
        if (component.equals(Form.getCurrentForm().getComponentByName("comboBox"))) {
            javax.swing.JLabel label = (javax.swing.JLabel)Form.getCurrentForm().getComponentByName("Message");
            label.setText(((javax.swing.JComboBox)Form.getCurrentForm().getComponentByName("comboBox")).getSelectedItem().toString());
            
            javax.swing.JTextArea textArea = (javax.swing.JTextArea)Form.getCurrentForm().getComponentByName("TextArea");
            textArea.setText(((javax.swing.JComboBox)Form.getCurrentForm().getComponentByName("comboBox")).getSelectedItem().toString());
        }      
    }
      
    @Override
    public void actionPerformed(java.awt.event.ActionEvent e){
        try {
            String action = e.getActionCommand();
            switch(action){
                case "Confirm":
                    javax.swing.UIManager.setLookAndFeel(((javax.swing.JComboBox)Form.getCurrentForm().getComponentByName("comboBox")).getSelectedItem().toString());
                    javax.swing.SwingUtilities.updateComponentTreeUI(Form.getCurrentForm().getFrame());
                    Form.getCurrentForm().getFrame().pack();
                    break;
                case "Tips":
                    ((javax.swing.JTextArea)Form.getCurrentForm().getComponentByName("TextArea")).setText("You can change JSwing default by setting up the LookAndFeel in static block of class");
                    break;
                case "Exit":
                    System.exit(0);
                    break;
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(SetLookAndFeel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(SetLookAndFeel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(SetLookAndFeel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(SetLookAndFeel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

 

Code Explained

  1. JComboBox initialization.

    UIManager.getInstalledLookAndFeels is used to get all installed LookAndFeel as JComboBox data source, then users can choose one to update LookAndFeel of JSwing applications.

  2. JComboBox event handler.

    When users select one of the JComboBox items, itemStateChanged will be fired.

 

Run application

SetLookAndFeel

 

Exercises after this tutorial

  1. Make an application to change the LookAndFeel style with JRadioButton.
  2. Modify LookAndFeel style via a static block of classes on start-up in your JSwing applications.

Next steps

Great! Once you've completed the SetLookAndFeel application, Click download source code or next for more tutorials.

Share this page

Let more people know about this site, please share