How do you change the size and font of a joptionpane?

neo999 picture neo999 · Nov 13, 2014 · Viewed 18.5k times · Source

Can you change the font and size of the text of a JOptionPane? I tried it and it works only if I "run file" on that specific java class. If you start the whole project it does not change the font. I only want to change only a specific JOptionPane not all of them.

Here is the code:

 UIManager.put("OptionPane.messageFont", new FontUIResource(new Font(  
          "Arial", Font.BOLD, 18)));       
 JOptionPane.showMessageDialog(null,"MESSAGE","ERROR",JOptionPane.WARNING_MESSAGE);         

Answer

Sergiy Medvynskyy picture Sergiy Medvynskyy · Nov 13, 2014

It's really easy. JOption pane accepts not only strings but also components. So you can create a label set its font and use it as message.

JLabel label = new JLabel("MESSAGE");
label.setFont(new Font("Arial", Font.BOLD, 18));
JOptionPane.showMessageDialog(null,label,"ERROR",JOptionPane.WARNING_MESSAGE);

I don't understand why nobody answered this question before