Change the look and feel : Look Feel « Swing JFC « Java






Change the look and feel

Change the look and feel
 

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class PlafTest extends JPanel implements ActionListener {
  private JButton metalButton = new JButton("Metal");

  private JButton motifButton = new JButton("Motif");

  private JButton windowsButton = new JButton("Windows");

  public PlafTest() {

    add(metalButton);
    add(motifButton);
    add(windowsButton);

    metalButton.addActionListener(this);
    motifButton.addActionListener(this);
    windowsButton.addActionListener(this);
  }

  public void actionPerformed(ActionEvent evt) {
    Object source = evt.getSource();
    String plaf = "";
    if (source == metalButton)
      plaf = "javax.swing.plaf.metal.MetalLookAndFeel";
    else if (source == motifButton)
      plaf = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
    else if (source == windowsButton)
      plaf = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
    try {
      UIManager.setLookAndFeel(plaf);
      SwingUtilities.updateComponentTreeUI(this);
    } catch (Exception e) {
    }
  }

  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setTitle("PlafTest");
    frame.setSize(300, 200);
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });

    Container contentPane = frame.getContentPane();
    contentPane.add(new PlafTest());

    frame.show();
  }
}
           
         
  








Related examples in the same category

1.Getting and Setting a Native Look and Feel
2.Retrieve the cross-platform look and feel
3.Default look and feel can be set in a file called 'swing.properties' located in the '/lib' directory.
4.Getting and Setting a Look and Feel
5.Set the look and feel using a system property on the command line
6.Look and feel string
7.Selecting different looks and feels
8.A Look-and-feel switcherA Look-and-feel switcher
9.Simple look and feel Example
10.Change Look and feelChange Look and feel
11.Get Installed Look And FeelsGet Installed Look And Feels
12.Get Swing Properties
13.awt.font.desktophints
14.Highlighted ButtonHighlighted Button