This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes How to change the font of JOptionPane? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to change the font of JOptionPane?" Watch "How to change the font of JOptionPane?" New topic

How to change the font of JOptionPane?

Bigwood Liu
Ranch Hand

Joined: Feb 26, 2003
Posts: 240
I want to change the font of buttons in JOptionPane, can you help me?
achana chan
Ranch Hand

Joined: Jul 29, 2002
Posts: 277
Try this :
Font txtFont = new Font("Monospaced", Font.PLAIN,11);
Hope it helps...

humanum errare est.
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

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.

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Bigwood Liu
Ranch Hand

Joined: Feb 26, 2003
Posts: 240
Thank you Nathan, I think a better way is to create my own dialog.
I agree. Here's the link:
subject: How to change the font of JOptionPane?
It's not a secret anymore!