Swing Examples - Look and feel of a window as current System



Following example showcase how to set the look and feel of a window as current System in Swing based application.

We are using the following APIs.

  • UIManager.getSystemLookAndFeelClassName() − To get the Look and Feel of Current System.

  • UIManager.setLookAndFeel() − To set the look and feel of UI components.

  • JFrame.setDefaultLookAndFeelDecorated(true); − To change the look and feel of the frame.

Example

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.LayoutManager;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;

public class SwingTester {
   public static void main(String[] args) {
      try {
         UIManager.setLookAndFeel(
         UIManager.getSystemLookAndFeelClassName());
      } catch (Exception e) { 
   }
      JFrame.setDefaultLookAndFeelDecorated(true);
      createWindow();   
   }
   
   private static void createWindow() {    
      JFrame frame = new JFrame("Swing Tester");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      createUI(frame);
      frame.setSize(560, 200);      
      frame.setLocationRelativeTo(null);  
      frame.setVisible(true);
   }

   private static void createUI(JFrame frame){      
      JPanel panel = new JPanel();
      LayoutManager layout = new FlowLayout();  
      panel.setLayout(layout);       
      panel.add(new JLabel("Hello World!"));

      frame.getContentPane().add(panel, BorderLayout.CENTER);    
   }
}

Output

look and feel of a current system
swingexamples_frames.htm
Advertisements