0

I'm attempting to use the UIManager in java to make all my pop up GUIs and error GUIs use the same colors. The issue is I can't seem to change the button color when using the windows style, as well I can't manage to change the GUIs title bar.

enter image description here

Code: Its rather simple, I'm calling UIManager.getLookAndFeelDefaults().put("Button.background", buttonColor) anda few other UI changes. Nothing major...

DarkGuardsman
  • 126
  • 4
  • 15

2 Answers2

2

UIManager.getLookAndFeelDefaults().put("Button.background", buttonColor)

For the color of the buttons from JOptionPane dialogs I don't understand why it doesn't work. It should. Maybe a side effect. We don't see all your code.
Try this simple code, you should see only green buttons.

package swing.uimanager;

import java.awt.Color;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIDefaults;
import javax.swing.UIManager;

public class JFrameWithUIManger extends JFrame {

    public static void main(String[] args) {
        new JFrameWithUIManger();
    }

    public JFrameWithUIManger() {
      final UIDefaults lookAndFeelDefaults = UIManager.getLookAndFeelDefaults();
      lookAndFeelDefaults.put("Button.background", Color.GREEN);

      add(new JButton("A button"));
      pack();
      setVisible(true);
      JOptionPane.showMessageDialog(this, "hello I am a message dialog");
      JOptionPane.showConfirmDialog(this, "hello I am a question dialog", "do you like me ?", JOptionPane.YES_NO_OPTION);
    }
}

For the white background in the title, you have a color (white), so it seems to work. Maybe, a problem in a key-value. Post it please if you want we understand better.

Edit

After seeing you code, I understood your problem. It's the look and feel used. You don't use the default look and feel (metal).
Probably, you don't know but all the look and feels in Swing don't born equals.
In deed, some options and mix-options are supported by some look and feels but are not supported by other look and feels.
If you use the default look and feel, you should have less compatibility problem.
The best way to know if it correctly supported is to report to some official docs but it's true that a lot of information about it is dry, with errors, and not detailed

In the link you posted in your comment : http://nadeausoftware.com/articles/2008/11/all_ui_defaults_names_common_java_look_and_feels_windows_mac_os_x_and_linux#Button, the keys displayed in the tables don't mean that values are present or modifiable but that only the keys are present in the LAF.
Each LAF is responsible to take into consideration or not to the key and the possibility to change values associated to the keys.

I tried with the Windows LAF to set only the background color button, it doesn't work either. So, it doesn't seem to be a side effect but it looks like more a missing of support on this feature for the Windows LAF.

Anyway, you can interest to the Nimbus if you want an official, nice and more recent and more flexible look and feel than the classic metal look and feel. Nimbus document
It's available since the Java SE 6 Update 10.

I agree with you, Metal is ugly. But why don't use nimbus instead of the windows LAF ? It's great. I tried your code which modifies some values of lAF with Nimbus, it seems working nicely. Maybe, you could have some minor modifications to adjust it but the basis should be nice.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • I create the JOptionPane s in the same way and here is my UI code https://gist.github.com/DarkGuardsman/9820dedcfd6dbdfbd4138bf0df3007b9 the rest is closed source so I can't share everything. However, I do think it is a side effect as I'm getting other odd errors now. For example JPanels that will not turn transparent. – DarkGuardsman Jul 30 '16 at 21:07
  • The default metal look and feel is visually bad, something along the lines of windows 2000 graphics. Which is why I was using windows style which several documents say supports changing the background colors. As well switching the metal look and feel resizes my buttons to be much smaller. http://nadeausoftware.com/articles/2008/11/all_ui_defaults_names_common_java_look_and_feels_windows_mac_os_x_and_linux#Button – DarkGuardsman Jul 30 '16 at 23:25
  • :/ bit odd on how all these UIManager segments are handled, you would expect some level of consistency between them. As well basic functionality like changing colors. As for nimbus its not what I'm looking for in a UI and it changes the over all visuals of the rest of the GUIs too much. Plus I would need to change all my key entries as I've heard nimbus keys are different. Thank you for the help, your comments were useful – DarkGuardsman Aug 01 '16 at 02:35
  • Indeed, LAFs have not consistency between them. The intention was good but the implementation not very good. Personally, I tried to use it. With the classic LAF, it's rather good. With Nimbus, it's rather good. I never tried the windows LAF but I would not advise it now :) You are welcome, see you :) – davidxxx Aug 01 '16 at 17:12
1

If you are using JOptionPane,

You an change the title using this format to use your own title.

JOptionPane.showMessageDialog(null, "This is the message", "This is the title", JOptionPane.YES_NO_OPTION);

To set back ground color of dialog-

UIManager UI=new UIManager();
UI.put("OptionPane.background", Color.white);
UI.put("Panel.background", Color.white);
JOptionPane.showMessageDialog(null,"Text","SetColor",JOptionPane.YES_NO_OPTION);

For changing buttons in dialog or everything else, create your own JDialog and set the button characteristics which you want.

Sirsendu
  • 283
  • 1
  • 8