views:

221

answers:

1

Hi I have created sample progeam which will give same look and feel of a confirm dialog and set the background colour as red. But I donot know what is the problem that my background colour of option is displayed as the default colour but not red. also i need same look and feel of confirm dialog across all platforms.

Here is the code that I have written. please help me to solve the issue

public class JOptionPaneBackground {

public static void main(String[] args) throws Exception {
    // UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

    UIDefaults uiDefaults = UIManager.getLookAndFeelDefaults();
    List<Object> keys = new ArrayList<Object>(uiDefaults.keySet());
    Collections.sort(keys, new Comparator<Object>() {
        public int compare(Object o1, Object o2) {
            return (o1.toString()).compareTo(o2.toString());
        }
    });
    for (Object key : keys) {
        System.out.println(String.format("%-40s = %s", key, uiDefaults.get(key)));
    }

    UIManager.put("OptionPane.background", Color.red);
    UIManager.put("Panel.background", Color.red);

    JOptionPane.showConfirmDialog(null, "Hello World!");
}

}

+1  A: 

I think you need to get a new instance of UIManager and set the color property of the pane on that.

Look here for the code snippet

This code works fine for me on a windows machine:

public class JOptionPaneBackground {

public static void main(String[] args) throws Exception {
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

    UIManager.put("OptionPane.background", Color.RED);
    UIManager.put("Panel.background", Color.RED);

    JOptionPane.showConfirmDialog(null, "Hello World!");
}

}

Prachi
Hi Prachi thatnks for your quick response. Nice to get response from you. In my code if I commentUIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); then my program works fine but that will not give same look and feel in all platforms. but i want both things cross platform look and feel and set background color also
Deepak
What version of Java are you using? I just tried your code on 1.5 and it works for me. I did comment out the part of your code that is displaying all the preferences since I am guessing that is just for testing.
Prachi
I am using Java 1.6
Deepak
i am using 64 bit Fedora. It does not work there
Deepak
I appreciate ur effort.
Deepak