Search code examples
javaswingnetbeansfontsjoptionpane

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


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);         

Solution

  • 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