Search code examples
javaswingappletlook-and-feelnimbus

When I run a JApplet got different colors for the same code


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


Solution

  • 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.