Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Set swing defaults

826367Dec 23 2010 — edited Dec 24 2010
Hi all,

I've been trying to change the default colours and fonts of a swing application - using UIManager.put().

Following is a test class wherein I'm trying to change the background colour of a menu to Red. This doesnt seem to be effective. Can someone please direct me on what the issue is...

Probably the system look and feel is over-riding my settings. Is it not possible to over-ride the system look and feel with my settings?
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.UIManager;
import javax.swing.plaf.ColorUIResource;

public class MyFrame extends JFrame {
    private static final String MENU_BACKGROUND_KEY = "MenuItem.background";
    private static final Color MENU_BACKGROUND_COLOR = Color.RED;
    
    public MyFrame() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        UIManager.put(MENU_BACKGROUND_KEY, new ColorUIResource(MENU_BACKGROUND_COLOR));
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("My Frame");
        setLocationRelativeTo(null);
        setPreferredSize(new Dimension(250, 250));
        setMinimumSize(new Dimension(250, 250));
        setMaximumSize(new Dimension(250, 250));
        
        JMenuBar menuBar = new JMenuBar();
        JMenu menu = new JMenu("Menu1");
        JMenuItem menuItem = new JMenuItem("New");
        menu.add(menuItem);
        menuBar.add(menu);
        setJMenuBar(menuBar);
        setVisible(true);
    }
    
    public static void main(String[] args) {
        new MyFrame();
    }
}
Thank you.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 21 2011
Added on Dec 23 2010
5 comments
316 views