Java Reference
In-Depth Information
{
}
Suppose your application provides a menu that lets the user choose the GUI's look
and feel. After selecting the menuitem, the visible GUI must be updated to reflect the
choice.Swingletsyouaccomplishthistaskfromthemenuitem'sactionlistener(orfrom
somewhere else on the EDT) as follows:
try
{
UIMan-
ager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
SwingUtilities.updateComponentTreeUI(frame);
frame.pack();
}
catch (Exception e)
{
}
The
javax.swing.SwingUtilities
class declares a
void updateCom-
ponentTreeUI(Component c)
class method that changes the look and feel by
invoking the
void updateUI()
method of each component located in the tree of
componentsrootedin
c
,whichtypicallyreferencesaframewindow.
updateUI()
in-
vokes
UIManager
's
ComponentUI getUI(JComponent target)
methodto
returnthenewlookandfeel'sUIdelegate,andpassesthisdelegatetothecomponent's
setUI()
method.Forexample,
JButton
's
updateUI()
methodisimplementedas
follows:
public void updateUI()
{
setUI((ButtonUI) UIManager.getUI(this));
}
frame.pack();
resizes components to their preferred sizes because these sizes
will probably change under the new look and feel.
Note
FormoreinformationonPLAFs,checkoutTheJavaTutorial's“Modifyingthe
Look and Feel” lesson (
http://download.oracle.com/javase/tutori-