Reputation: 12757
I've written a JApplet and I set, in the initialization, colors for the Nimbus L&F.
When I run the applet, both via Netbeans or via Google Chrome, 9/10 times it happens that button background is set to dark, but sometimes Nimbus is not able to set the color.
Here is a SSCCE:
import java.awt.Color;
import java.lang.reflect.InvocationTargetException;
import javax.swing.UIManager;
public class NimbusColors extends javax.swing.JApplet {
// colors and look and feel
Color buttonBackgroundColor;
Color buttonTextColor;
Color textAreaBackgroundColor;
Color textAreaTextColor;
Color skinColor;
Color defaultButtonBackgroundColor = Color.decode("#4a4a4a");
Color defaultButtonTextColor = Color.decode("#cecece");
Color defaultTextAreaBackgroundColor = Color.decode("#414141");
Color defaultTextAreaTextColor = Color.decode("#cecece");
Color defaultSkinColor = Color.decode("#353535");
Color progressColor = Color.decode("#00FFFF");
@Override
public void init() {
getContentPane().setBackground(defaultSkinColor);
UIManager.put("TextArea.background", defaultTextAreaBackgroundColor);
UIManager.put("TextArea.foreground", defaultTextAreaTextColor);
UIManager.put("control", defaultTextAreaBackgroundColor);
UIManager.put("nimbusLightBackground", defaultSkinColor);
UIManager.put("background", defaultSkinColor);
UIManager.put("text", defaultButtonTextColor);
UIManager.put("ComboBox.background", defaultSkinColor.darker().darker());
UIManager.put("Button.background", defaultSkinColor);
UIManager.put("info", defaultSkinColor.brighter().brighter());
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NimbusColors.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
/* Create and display the applet */
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
@Override
public void run() {
initComponents();
}
});
} catch (InterruptedException | InvocationTargetException ex) {
System.exit(11);
}
}
private void initComponents() {
jButtonBrowseFS = new javax.swing.JButton();
jButtonBrowseFS.setText("Browse");
jButtonBrowseFS.setToolTipText("Browse your filesystem to select files to upload");
jButtonBrowseFS.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
jButtonBrowseFS.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButtonBrowseFS)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButtonBrowseFS)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
}
private javax.swing.JButton jButtonBrowseFS;
}
I've tried this code with Netbeans 7.3.1, creating a new Java Project and adding a new JApplet file. If I run the file from Netbeans a dozen times, sometimes button background is dark, sometimes it's not.
Can anyone replicate this strange behavior? What is going on?
Update 1: I'm running Windows 8 Pro
Upvotes: 2
Views: 321
Reputation: 12757
I've finally found a workaround.
In netbeans, I've set the background property of the button to some value (different from the one I want, but different from the default 240,240,240 too).
When I run the applet, now I always get what I expect, that is the color set in the code with Nimbus.
Upvotes: 0
Reputation: 109813
for JButton is there used Painter, Background is ignored by default
there aren't changes betweens Java6/7
not all Keys works as we expected, Nimbus has a lots of suprices (solved in two-three custom L&F based on Nimbus)
one of ways, works for me in all cases, intentionally delayed by using Swing Timer, for example
Color.decode("#353535");
returns
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.LookAndFeel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.UnsupportedLookAndFeelException;
public class NimbusTest3 {
private static final long serialVersionUID = 1L;
private javax.swing.JButton button;
private JFrame frame = new JFrame();
private Timer t;
public NimbusTest3() {
button = new javax.swing.JButton();
button.setText("Text");
frame.add(button);
frame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
t = new Timer(1000, new ActionListener() {
private Random r = new Random();
@Override
public void actionPerformed(ActionEvent e) {
Color c = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
try {
LookAndFeel lnf = UIManager.getLookAndFeel().getClass().newInstance();
UIDefaults uiDefaults = lnf.getDefaults();
uiDefaults.put("nimbusBase", c);
UIManager.getLookAndFeel().uninitialize();
UIManager.setLookAndFeel(lnf);
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
UIDefaults defaults = UIManager.getDefaults();
defaults.put("Button.background", c);
SwingUtilities.updateComponentTreeUI(button);
t.stop();
}
});
t.start();
}
public static void main(String args[]) {
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
return;
}
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
NimbusTest3 nimbusTest3 = new NimbusTest3();
}
});
}
}
Upvotes: 2
Reputation: 5236
I tried your code but It always shows same colors. I think there is a problem with your Netbeans or jdk version. I am using Netbeans 7.3 and jdk 1.6.
Upvotes: 2