Java Reference
In-Depth Information
Example 10•1: ShowComponent.java (continued)
/**
* This static method queries the system to find out what Pluggable
* Look-and-Feel (PLAF) implementations are available. Then it creates a
* JMenu component that lists each of the implementations by name and
* allows the user to select one of them using JRadioButtonMenuItem
* components. When the user selects one, the selected menu item
* traverses the component hierarchy and tells all components to use the
* new PLAF.
**/
public static JMenu createPlafMenu(final JFrame frame) {
// Create the menu
JMenu plafmenu = new JMenu("Look and Feel");
// Create an object used for radio button mutual exclusion
ButtonGroup radiogroup = new ButtonGroup();
// Look up the available look and feels
UIManager.LookAndFeelInfo[] plafs =
UIManager.getInstalledLookAndFeels();
// Loop through the plafs, and add a menu item for each one
for(int i = 0; i < plafs.length; i++) {
String plafName = plafs[i].getName();
final String plafClassName = plafs[i].getClassName();
// Create the menu item
JMenuItem item = plafmenu.add(new JRadioButtonMenuItem(plafName));
// Tell the menu item what to do when it is selected
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
// Set the new look and feel
UIManager.setLookAndFeel(plafClassName);
// Tell each component to change its look-and-feel
SwingUtilities.updateComponentTreeUI(frame);
// Tell the frame to resize itself to the its
// children's new desired sizes
frame.pack();
}
catch(Exception ex) { System.err.println(ex); }
}
});
// Only allow one menu item to be selected at once
radiogroup.add(item);
}
return plafmenu;
}
/**
* This method loops through the command line arguments looking for
* class names of components to create and property settings for those
* components in the form name=value. This method demonstrates
* reflection and JavaBeans introspection as they can be applied to
* dynamically created GUIs
**/
Search WWH ::




Custom Search