Java - Change Look And Feel of Swing applications

This java class LookAndFeelJMenu extends the swing component JMenu which can be added directly to a JMenuBar.
LookAndFeelJMenu will show a list of all LookAndFeels that are installed in your JRE.
Selecting a menu item from the menu will cause your swing application to switch to that LookAndFeel immediately.

package Livares;

import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

/**
 *
 * @author Livares
 */
public class LookAndFeelJMenu extends JMenu {

    public LookAndFeelJMenu(String name,final java.awt.Component rootComponent){
       super(name);
       final UIManager.LookAndFeelInfo looks[] = UIManager.getInstalledLookAndFeels();
       for(int i=0;i<looks.length;i++){
            final int index=i;
            JMenuItem theme = new JMenuItem();
            theme.setText(looks[index].getName());
            theme.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                try {
                    UIManager.setLookAndFeel(looks[index].getClassName());
                    SwingUtilities.updateComponentTreeUI(rootComponent);
                } catch (Exception ex) {
                    System.out.println(ex.getMessage());
                }
            }
        });
            this.add(theme);
            System.out.println();
       }
    }

You can construct an object of this class and add it to Menu Bar using JMenuBar.add() menthod.
        javax.swing.JMenuBar MyMenuBar=new javax.swing.JMenuBar();
        MyMenuBar.add(new Livares.LookAndFeelJMenu("Select Theme", rootComponent));
You may have noticed that the constructor takes two parameters. First parameter is a String which will appear as name of the Menu. Second parameter is your roor component. This will be usually a reference to your main window frame.
UI style of this component and its component sub-tree is updated automatically whenever an item in LookAndFeelJMenu is clicked.

Here's a sample JFrame with LookAndFeelJMenu implemented in its menu bar.
   public static class SampleFrame extends javax.swing.JFrame{
      public SampleFrame() {
          super();
          setSize(500, 550);
          setResizable(false);

        javax.swing.JMenuBar Main_jMenuBar=new javax.swing.JMenuBar();
        Main_jMenuBar.add(new Livares.LookAndFeelJMenu("Select Theme", this));
        setJMenuBar(Main_jMenuBar);
        
        javax.swing.JScrollPane jScrollPane = new javax.swing.JScrollPane();
        jScrollPane.setSize(400, 400);
        javax.swing.JTable jTable = new javax.swing.JTable(
                   new Object [][] {{"Lorem", "ipsum","dolor", "sit amet"},
                                   {"Lorem", "ipsum","dolor", "sit amet"},
                                   {"Lorem", "ipsum","dolor", "sit amet"},
                                   {"Lorem", "ipsum","dolor", "sit amet"},
                                                                    },
                   new String [] {"Column 1", "Column 2", "Column 3", "Column 4"}
                                                   );

        jScrollPane.setViewportView(jTable);
        java.awt.FlowLayout layout=new java.awt.FlowLayout();
        getContentPane().setLayout(layout);
        layout.setAlignment(java.awt.FlowLayout.CENTER);

        getContentPane().add(new javax.swing.JButton("Sample button"));
        getContentPane().add(new javax.swing.JTextField("Sample TextField"));
        getContentPane().add(new javax.swing.JComboBox(new Object[]{"Sample","ComboBox"}));
        getContentPane().add(jScrollPane);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
      }

      public static void main(String[] args) {
            SampleFrame sample=new SampleFrame();
            sample.setVisible(true);
      }
   }

Popular posts from this blog

Store your 450GB data on an A4 size paper: THE RAINBOW TECHNOLOGY

Java XML getElementById returning null, fix using XPath

Software Development Models