Java Reference
In-Depth Information
a call to the public static void updateComponentTreeUI(Component rootComponent) method of
SwingUtilities . If the component hasn't been created yet, this isn't necessary.
The following source fragment demonstrates changing a look and feel:
try {
UIManager.setLookAndFeel(finalLafClassName);
SwingUtilities.updateComponentTreeUI(frame);
} catch (Exception exception) {
JOptionPane.showMessageDialog (
frame, "Can't change look and feel",
"Invalid PLAF", JOptionPane.ERROR_MESSAGE);
}
Figure 20-2 illustrates the results of a demonstration program that can change the look
and feel at runtime through either a JComboBox or JButton component. Frequently, you won't
want to allow a user to change the look and feel; you may just want to set the look and feel at
startup time.
Figure 20-2. Before and after changing the look and feel
Listing 20-2 shows the complete source of the program shown in Figure 20-2.
Listing 20-2. Changing the Look and Feel
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.plaf.*;
public class ChangeLook {
public static void main (String args[]) {
Runnable runner = new Runnable() {
public void run() {
final JFrame frame = new JFrame("Change Look");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Search WWH ::




Custom Search