Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to change font for a JOptionPane

 
Rosie Fairfield
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am quite familiar with using JOptionPane and its various displayXDialog() methods and reasonably familiar with changing fonts.

I have a JOptionPane.displayMessageDialog() that is working fine but I decided to make the font larger.

I found an example from which I coded:

After the UIManager.getFont() call after the UIManager.put() call, font shows the new font size of 24, but the showMessageDialog() dialog still has the default font.

And yes I understand that, when this works, it will affect every JOptionPane in my program.

I also tried:

This gave me a dialog with the default font and an unwanted icon.

So I tried

and planned to use that font in my setFont() call but font was null.

Can anyone help?

Thanks.
 
Campbell Ritchie
Marshal
Posts: 52663
121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you create an object of jOptionPane rather than using its static methods, and apply the font to that object?
 
Rosie Fairfield
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell,

No. See the second code snippet where I tried that.

Thanks for responding.

Rosie
 
Campbell Ritchie
Marshal
Posts: 52663
121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome

Since I haven't done much Swing for ages, I thought I'd try it out. If you have a JOptionPane object, it has a setFont method, but I am not sure how you get such an object to appear and disappear the way you do with the static showXXXDialog methods. So I tried it with some Java8 features (lines 15, 34 and 42) and a JDialog objectjava DialogueDemo "Campbell is Brilliant" "This is how you show a Dialogue"
That looks exactly the way you would change the font on a label on a JFrame or similar.
 
Rosie Fairfield
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell,

Thank you again

This worked and has the advantage of allowing me to set the font of each dialog separately and you showed me a new way to create dialogs that is easier than how I used to do it.

BUT, changing fonts in a plain JOptionPane should be possible and as easy as changing fonts in the JLabel.

Rosie
 
Campbell Ritchie
Marshal
Posts: 52663
121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Tony Docherty
Saloon Keeper
Posts: 3145
74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BUT, changing fonts in a plain JOptionPane should be possible and as easy as changing fonts in the JLabel.

They are not really comparable situations. With a JLabel you create the object, set it up, and then display it. With the static factory methods of JOptionPane you call one of them and it creates, sets up and displays the dialog and the dialog is modal so you can't get to change any of the settings.

You can however change the default font the JOptionPane will use when creating a dialog by setting the UIManager parameters as follows:

Note this is a global change which will persist for the every subsequent call to create a JOptionPane unless you reset the values to the original values.

Edit: Just noticed the OP was already doing this - serves me right for not reading the whole thread before answering
 
I just had the craziest dream. This tiny ad was in it.
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!