Changing the look-and-feel. - Java Swing

Java examples for Swing:Look and Feel

Description

Changing the look-and-feel.

Demo Code

import javax.swing.JFrame;

import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

 class LookAndFeelFrame extends JFrame 
{
   private final UIManager.LookAndFeelInfo[] looks; 
   private final String[] lookNames;
   private final JRadioButton[] radio; 
   private final ButtonGroup group;
   private final JButton button;
   private final JLabel label;
   private final JComboBox<String> comboBox;

   public LookAndFeelFrame()
   {//from  w ww.j a va 2s  .c o  m
      looks = UIManager.getInstalledLookAndFeels();
      lookNames = new String[looks.length];

      // get names of installed look-and-feels
      for (int i = 0; i < looks.length; i++)
         lookNames[i] = looks[i].getName();

      JPanel northPanel = new JPanel(); 
      northPanel.setLayout(new GridLayout(3, 1, 0, 5));

      label = new JLabel("This is a " + lookNames[0] + " look-and-feel",
         SwingConstants.CENTER); 
      northPanel.add(label); 

      button = new JButton("JButton");
      northPanel.add(button);

      comboBox = new JComboBox<String>(lookNames); 
      northPanel.add(comboBox);
     
      // create array for radio buttons
      radio = new JRadioButton[looks.length];

      JPanel southPanel = new JPanel(); 

      // use a GridLayout with 3 buttons in each row
      int rows = (int) Math.ceil(radio.length / 3.0);
      southPanel.setLayout(new GridLayout(rows, 3));

      group = new ButtonGroup(); // button group for look-and-feels
      ItemHandler handler = new ItemHandler(); // look-and-feel handler

      for (int count = 0; count < radio.length; count++) 
      {
         radio[count] = new JRadioButton(lookNames[count]);
         radio[count].addItemListener(handler); // add handler
         group.add(radio[count]); // add radio button to group
         southPanel.add(radio[count]); // add radio button to panel
      } 

      add(northPanel, BorderLayout.NORTH); // add north panel
      add(southPanel, BorderLayout.SOUTH); // add south panel

      radio[0].setSelected(true); // set default selection
   } // end LookAndFeelFrame constructor

   private void changeTheLookAndFeel(int value)
   {
      try
      {
         UIManager.setLookAndFeel(looks[value].getClassName());

         SwingUtilities.updateComponentTreeUI(this);
      } 
      catch (Exception exception) 
      {
         exception.printStackTrace();
      }
   }

   private class ItemHandler implements ItemListener 
   {
      @Override
      public void itemStateChanged(ItemEvent event)
      {
         for (int count = 0; count < radio.length; count++)
         {
            if (radio[count].isSelected()) 
            {
               label.setText(String.format(
                   "This is a %s look-and-feel", lookNames[count]));
               comboBox.setSelectedIndex(count); // set combobox index
               changeTheLookAndFeel(count); // change look-and-feel
            }
         } 
      } 
   } 
}

public class Main 
{
   public static void main(String[] args)
   { 
      LookAndFeelFrame lookAndFeelFrame = new LookAndFeelFrame();  
      lookAndFeelFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      lookAndFeelFrame.setSize(400, 220); 
      lookAndFeelFrame.setVisible(true); 
   } 
}

Related Tutorials