Dynamically change the Look and Feel of a Swing application

Invoke UIManager.setLookAndFeel and provide it with the new Look and Feel class. Force components to reflect this new look by calling SwingUtilities.updateComponentTreeUI. All components in the tree of which you specify the parent component will be asked to use the new Look And Feel. Also do a call to frame.pack to ensure the window to be sized to fit the preferred size and layouts of its subcomponents. Here’s an example:

import javax.swing.table.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
 
public class Main implements ActionListener
{ 
   JFrame frame;
   
   public Main() {
      frame = new JFrame("Table one row selection demonstration"); 
      frame.addWindowListener( new WindowAdapter() { 
         public void windowClosing(WindowEvent e) { 
            System.exit(0); 
         } 
      }); 
      JMenuBar menuBar = new JMenuBar();
      JMenu menu = new JMenu("Look and Feel");
      JMenuItem menuItem1 = new JMenuItem("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
      JMenuItem menuItem2 = new JMenuItem("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
      JMenuItem menuItem3 = new JMenuItem("javax.swing.plaf.metal.MetalLookAndFeel");
      menu.add(menuItem1);
      menu.add(menuItem2);
      menu.add(menuItem3);
      menuItem1.addActionListener(this);
      menuItem2.addActionListener(this);
      menuItem3.addActionListener(this);
      menuBar.add(menu);
      frame.setJMenuBar(menuBar);
 
      JTable table = new JTable(35, 3); 
  
      table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
 
      frame.getContentPane().add(new JScrollPane(table)); 
 
      frame.pack(); 
      frame.show(); 
   } 
 
   public void actionPerformed(ActionEvent ae) {
      try {
         UIManager.setLookAndFeel(ae.getActionCommand());
         SwingUtilities.updateComponentTreeUI(frame);
         frame.pack();
      }
      catch(Exception e) {
         System.out.println(e);
      }
   }
 
   public static void main(String[] args) {
      Main main = new Main();
   }
}