Whenever the user of the application mouses over a different button, the entire application is updated in terms of its look and feel.
public class LAFSwitchButton extends JButton {
public LAFSwitchButton(final String laf) {
setText(laf);
setPreferredSize(new Dimension(130,40));
addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
NbPreferences.forModule(LAFSwitchButton.class).put("laf", laf);
setBorder(new LineBorder(Color.RED,2));
}
@Override
public void mouseExited(MouseEvent e) {
setBorder(new LineBorder(Color.BLACK));
}
});
}
}
Now add a few of the above buttons to a TopComponent, get notified of preference changes, and then update the look and feel:
public final class LAFSwitchTopComponent extends TopComponent {
public LAFSwitchTopComponent() {
initComponents();
setName(NbBundle.getMessage(LAFSwitchTopComponent.class, "CTL_PersonasTopComponent"));
setToolTipText(NbBundle.getMessage(LAFSwitchTopComponent.class, "HINT_PersonasTopComponent"));
setLayout(new MigLayout());
add(new LAFSwitchButton("Nimbus"), "growx, push, wrap");
add(new LAFSwitchButton("Metal"), "growx, push, wrap");
add(new LAFSwitchButton("GTK"), "growx, push, wrap");
add(new LAFSwitchButton("Motif"), "growx, push, wrap");
final Preferences pref = NbPreferences.forModule(LAFSwitchButton.class);
pref.addPreferenceChangeListener(new PreferenceChangeListener() {
@Override
public void preferenceChange(final PreferenceChangeEvent evt) {
WindowManager.getDefault().invokeWhenUIReady(new Runnable() {
@Override
public void run() {
if (evt.getKey().equals("laf")) {
String laf = pref.get("laf", "");
if (laf.equals("Metal")) {
setLAF("javax.swing.plaf.metal.MetalLookAndFeel");
} else if (laf.equals("Nimbus")) {
setLAF("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} else if (laf.equals("GTK")) {
setLAF("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
} else if (laf.equals("Motif")) {
setLAF("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
}
}
}
private void setLAF(String laf) {
try {
UIManager.setLookAndFeel(laf);
JFrame frame = (JFrame) WindowManager.getDefault().getMainWindow();
SwingUtilities.updateComponentTreeUI(frame);
} catch (ClassNotFoundException ex) {
Exceptions.printStackTrace(ex);
} catch (InstantiationException ex) {
Exceptions.printStackTrace(ex);
} catch (IllegalAccessException ex) {
Exceptions.printStackTrace(ex);
} catch (UnsupportedLookAndFeelException ex) {
Exceptions.printStackTrace(ex);
}
}
});
}
});
}
...
...
...
That's all, and all of the above has always been possible, there's nothing new or 7.0-related in the code above; now mousing over one of the buttons will cause an immediate update of the look and feel of the application.
And there it is! Thanks to the code above I got my reinitializing problem described at http://netbeans.dzone.com/nb-nimbus-tweakings solved. Thanks!
Hi Geertjan,
you are posting really cool stuff.
Your blog is one of my favourites blog's.
Thank's a lot!
How can we wrap those buttons so that they can be added to the palette, be dragged to the designer and render as buttons during the design stage?
in netbeans 7.0 theres is an issue...
as we click on customize toolbars ...
a new build toolbar(disabled) is added in the application automatically..
please help or give suggestions...