public static javax.swing.JMenu | createLookAndFeelMenu(java.lang.Class prefsClass, java.awt.event.ActionListener listener)Create a menu of radio buttons listing the available Look and Feels.
When the user selects one, change the component hierarchy under frame
to the new LAF, and store the new selection as the current preference
for the package containing class c.
// Create the menu
final 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();
// Find out which one is currently used
String currentLAFName=UIManager.getLookAndFeel().getClass().getName();
// 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
final JMenuItem item =
plafmenu.add(new JRadioButtonMenuItem(plafName));
item.setSelected(plafClassName.equals(currentLAFName));
// Tell the menu item what to do when it is selected
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
// Set the new look and feel
try { UIManager.setLookAndFeel(plafClassName); }
catch(UnsupportedLookAndFeelException e) {
// Sometimes a Look-and-Feel is installed but not
// supported, as in the Windows LaF on Linux platforms.
JOptionPane.showMessageDialog(plafmenu,
"The selected Look-and-Feel is " +
"not supported on this platform.",
"Unsupported Look And Feel",
JOptionPane.ERROR_MESSAGE);
item.setEnabled(false);
}
catch (Exception e) { // ClassNotFound or Instantiation
item.setEnabled(false); // shouldn't happen
}
// Make the selection persistent by storing it in prefs.
Preferences p = Preferences.userNodeForPackage(prefsClass);
p.put(PREF_NAME, plafClassName);
// Invoke the supplied action listener so the calling
// application can update its components to the new LAF
// Reuse the event that was passed here.
listener.actionPerformed(event);
}
});
// Only allow one menu item to be selected at once
radiogroup.add(item);
}
return plafmenu;
|