views:

60

answers:

1

Hello,

I'm experiencing a subtle issue with the Swing SystemLookAndFeel under Windows 7. The applet below sets the SystemLookAndFeel and then modifies the background colour of MenuBar and MenuItem. This works perfectly well with Windows XP and it works also well with Windows 7 having the Windows Classic theme activated. But it has no effect with the Windows 7 standard theme. Does anyone have an explanation for it?

Regards, Martin.

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JApplet;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

@SuppressWarnings("serial")
public class Win7TestApplet extends JApplet {

    public void init() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            UIManager.put("MenuBar.background", Color.decode( "#efecea" ));
            UIManager.put("MenuItem.background", Color.decode( "#9999ff" ));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }

        // Setup panel
        JPanel mainPanel = new JPanel();
        mainPanel.setBackground( Color.white );
        mainPanel.setLayout( new BorderLayout() );
        mainPanel.setOpaque( true );
        this.getContentPane().add( mainPanel, BorderLayout.CENTER );

        // Create menubar
        JMenuBar menuBar = new JMenuBar();
        getContentPane().add(menuBar, BorderLayout.NORTH);

        // Add menu
        JMenu m_file = new JMenu( "File" );
        menuBar.add( m_file );

        // Add menu items
        m_file.add( new JMenuItem( "First item" ) );
        m_file.add( new JMenuItem( "Second item" ) );
    }

    public void start() {}
    public void stop() {}
    public void destroy() {}
}
+1  A: 

Windows 7 may use the NimbusLookAndFeel, which has its own defaults and a different way to define colors.

Addendum: If not, you may need to specify a ColorUIResource, for example

UIManager.put("MenuBar.background",
    new ColorUIResource(Color.decode("#efecea")));
trashgod
Thanks for the links, trashgod. Nimbus look and feel is installed but the system default is shown as "The Microsoft Windows Look and Feel - com.sun.java.swing.plaf.windows.WindowsLookAndFeel". Also, using ColorUIResource doesn't change anything. Did it work for you? Any other ideas?
Martin
trashgod
@trashgod: Thanks for your help. I'll try the Java Forum when I managed to reactivate my account. Or I find out how to file a bug with Oracle.
Martin