Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to change the font of JOptionPane?

 
Bigwood Liu
Ranch Hand
Posts: 240
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want to change the font of buttons in JOptionPane, can you help me?
Best,
 
achana chan
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this :
Font txtFont = new Font("Monospaced", Font.PLAIN,11);
myJButton.setFont(txtFont);
Hope it helps...
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wasn't sure if OptionPane.getOptions() returned an array of the buttons, or just the Strings, but when I tested it, it just returns 'null' unless you specifically set options yourself. You could get buttons from the BasicOptionPaneUI, but the method is protected, so it would be a pain to re-implement the entire OptionPaneUI just to get to this. You could also create your own buttons with the specified font, and pass them into the optionpane constructor as 'options' but then you'd have to implement the actionlisteners that should be attached to the buttons (option pane doesn't do this for you).

You could use a method like the following to get all the buttons, then go through the list and set the font on each one. This is really just hack (unfortunately though, I don't see a *good* way to do this). Pass the JButton.class class in and the option pane (or dialog, or internal frame created from the option pane). Then just go through the list you get back and call setFont() on each button.

 
Bigwood Liu
Ranch Hand
Posts: 240
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Nathan, I think a better way is to create my own dialog.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic