Reputation: 21
I would like use the nimbus button style in my application. I don't want change L&F. Only change the L&F of the button to use nimbus L&F. Is there a way to do this?
Upvotes: 1
Views: 2678
Reputation: 1633
There might be a better way, but the following utility class should work for you:
import javax.swing.JButton;
import javax.swing.LookAndFeel;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
public class NimbusButton {
private static LookAndFeel nimbus;
public static JButton generateNimbusButton() {
try {
LookAndFeel current = UIManager.getLookAndFeel(); //capture the current look and feel
if (nimbus == null) { //only initialize Nimbus once
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
nimbus = UIManager.getLookAndFeel();
}
else
UIManager.setLookAndFeel(nimbus); //set look and feel to nimbus
JButton button = new JButton(); //create the button
UIManager.setLookAndFeel(current); //return the look and feel to its original state
return button;
}
catch (Exception e) {
e.printStackTrace();
return new JButton();
}
}
}
The generateNimbusButton() method changes the look and feel to Nimbus, creates the button, then changes the look and feel back to whatever it was when the method was called.
Upvotes: 1