Java Swing JOptionPane Yes and No Button differ in font size after setting Nimbus LookandFeel
Asked Answered
N

2

5

Since the font size is too small in default LookAndFeel, I used a few lines of code to set it to Nimbus, and my JOptionPane shows Yes and No button with different size. Yes is still very small, while No is set to be with the size I assign it to be. Anybody knows why or how to fix it?

public static void setUIFont(Font a){
    FontUIResource ax=new FontUIResource(a);
    javax.swing.UIManager.put("OptionPane.messageFont", ax);
    javax.swing.UIManager.put("OptionPane.buttonFont", ax);
    javax.swing.UIManager.put("OptionPane.Font", ax);
    javax.swing.UIManager.put("InternalFrame.titleFont", ax);
    javax.swing.UIManager.put("TextField.font", ax);
    javax.swing.UIManager.put("ComboBox.font", ax);
}

...

UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); 

class UseNimBus extends SwingInvoker{
    public void run(){
        JDialog.setDefaultLookAndFeelDecorated(true);
        setUIFont(new FontUIResource(new Font(ufont, Font.PLAIN, 20)));
    }
}
(new UseNimBus()).execute();// just invokeLater()

The following line shows the option pane but it has Yes and No with different size. Is it normal or is it just my problem?

inputValuex=JOptionPane.showConfirmDialog(
    myWin, "Are you exiting?", "You clicked X", JOptionPane.YES_NO_OPTION);

Update

Still not working. I have tried to use the code

javax.swing.UIManager.put("OptionPane.isYesLast", true);

to change the location of the Yes button but it didn't have any effect. I just wanted to see how to set those values such as boolean.

Also, I even listed all keys in UIManager.getDefaults() which contains optionpane, or button, and their font sizes are all set to 20. The Yes button still as smaller font.

Nonetheless answered 19/3, 2013 at 2:24 Comment(1)
sounds like a bug, +1 for catching it :-)Cyrilcyrill
T
4

The font for the JButton is actually coming from the Button.font property.

If I add this to your list of properties, it works.

javax.swing.UIManager.put("Button.font", ax);

Why you get two different sizes, while funny, is beyond me at this moment.

Tran answered 19/3, 2013 at 2:30 Comment(1)
thanks to your suggestion. It did remind me to look at all the available properties. It's just weird.Nonetheless
C
4

Looks like the reason for not taking the custom font for the first button created in the optionPane is that it is made the defaultButton of the rootPane. Being so, it has a special state [Default] that is used for looking up the properties - iff the button's set font is of type UIResource.

So what happens is

  • OptionPaneUI sets the font of all its created buttons to the font it finds in the UIManager for "OptionPane.buttonFont"
  • SynthButtonUI ignores the button's font for state [Default] because it is a UIResource

For some reason that I don't know (but think to remember that the lookup for named/child components is broken in Nimbus), none of the settings below have any effect

UIManager.put("\"OptionPane.button\"[Default].font", ax);
UIManager.put("OptionPane:\"OptionPane.button\"[Default].font", ax);

Furthermore, the custom font of the second button is lost on rollover/focused when setting it to a UIResource. A hackaround is to not set it as UIResource, that is

public static void setUIFont(Font a){
    // force usage of the button's font as set by optionPaneUI
    // by _not_ making it a uiResource
    UIManager.put("OptionPane.buttonFont", a);
    // use uiResource for others
    FontUIResource ax=new FontUIResource(a);
    UIManager.put("OptionPane.messageFont", ax);
    ...
}

Normally, not the best of ideas. But we might get away with it here because the buttons are created on each call to JOptionPane.create, no lasting effect on the individual instance.

Cyrilcyrill answered 20/3, 2013 at 11:12 Comment(1)
I saw there a few times that required (sorry really forgot if my initial idea was about Borders or JMenuItems with diff. Fonts) UIManager.getLookAndFeel().uninitialize();Demi

© 2022 - 2024 — McMap. All rights reserved.