Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 861
Hai Bi Avatar asked Mar 19 '13 02:03

Hai Bi


People also ask

How do you change the font on JOptionPane?

put("OptionPane. buttonFont", new Font("Arial", Font. PLAIN, 12)); Just remember to set it before any JOptionPane dialog appears.

How do I change my runtime font?

For instance: Create a new Font from the FontFamily list, and apply it to your component with c. setFont (font); A second approach is, to search for TTF-Files (for example), and create new Fonts with the static method Font. createFont (new File ("..."));

What is initUI in Java?

public Example() { initUI(); } It is a good programming practice to put the code that creates the GUI inside a specific method.


1 Answers

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.

like image 136
MadProgrammer Avatar answered Sep 25 '22 12:09

MadProgrammer