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);
This is the way we shall use:
UIManager.getLookAndFeelDefaults().put("OptionPane.messageFont", new Font("Arial", Font.BOLD, 14));
UIManager.getLookAndFeelDefaults().put("OptionPane.buttonFont", new Font("Arial", Font.PLAIN, 12));
UIManager.put("OptionPane.messageFont", new Font("Arial", Font.BOLD, 14));
UIManager.put("OptionPane.buttonFont", new Font("Arial", Font.PLAIN, 12));
Just remember to set it before any JOptionPane dialog appears. I just put it in the first line of the main
method.
To see why I do this, the DOC of UIManager is always useful.
Defaults
UIManager manages three sets of UIDefaults. In order, they are:
Developer defaults. With few exceptions Swing does not alter the developer defaults; these are intended to be modified and used by the developer.
Look and feel defaults. The look and feel defaults are supplied by the look and feel at the time it is installed as the current look and feel (setLookAndFeel() is invoked). The look and feel defaults can be obtained using the getLookAndFeelDefaults() method.
System defaults. The system defaults are provided by Swing. Invoking any of the various get methods results in checking each of the defaults, in order, returning the first non-null value. For example, invoking UIManager.getString("Table.foreground") results in first checking developer defaults. If the developer defaults contain a value for "Table.foreground" it is returned, otherwise the look and feel defaults are checked, followed by the system defaults. It's important to note that getDefaults returns a custom instance of UIDefaults with this resolution logic built into it. For example, UIManager.getDefaults().getString("Table.foreground") is equivalent to UIManager.getString("Table.foreground"). Both resolve using the algorithm just described. In many places the documentation uses the word defaults to refer to the custom instance of UIDefaults with the resolution logic as previously described.
So, we should change first the developer defaults. And the method UIManager.put(Object key, Object value)
is the method to use.
public static Object put(Object key, Object value)
Stores an object in the developer defaults. This is a cover method for getDefaults().put(key, value). This only affects the developer defaults, not the system or look and feel defaults.
Parameters:
key - an Object specifying the retrieval key
value - the Object to store; refer to UIDefaults for details on how null is handled
Returns: the Object returned by UIDefaults.put(java.lang.Object, java.lang.Object)
Throws:
NullPointerException - if key is null
This is exactly what I am looking for: no extra panels, no more trouble of overriding the default UI of JOptionPane
.
A complete list of names of attributes in JOptionPane
is here:
http://www.java2s.com/Tutorial/Java/0240__Swing/CustomizingaJOptionPaneLookandFeel.htm
Property String Object Type
OptionPane.actionMap ActionMap
OptionPane.background Color
OptionPane.border Border
OptionPane.buttonAreaBorder Border
OptionPane.buttonClickThreshhold Integer
OptionPane.buttonFont Font
OptionPane.buttonOrientation Integer
OptionPane.buttonPadding Integer
OptionPane.cancelButtonMnemonic String
OptionPane.cancelButtonText String
OptionPane.cancelIcon Icon
OptionPane.errorDialog.border.background Color
OptionPane.errorDialog.titlePane.background Color
OptionPane.errorDialog.titlePane.foreground Color
OptionPane.errorDialog.titlePane.shadow Color
OptionPane.errorIcon Icon
OptionPane.errorSound String
OptionPane.font Font
OptionPane.foreground Color
OptionPane.informationIcon Icon
OptionPane.informationSound String
OptionPane.inputDialogTitle String
OptionPane.isYesLast Boolean
OptionPane.messageAnchor Integer
OptionPane.messageAreaBorder Border
OptionPane.messageFont Font
OptionPane.messageForeground Color
OptionPane.messageDialogTitle String
OptionPane.minimumSize Dimension
OptionPane.noButtonMnemonic String
OptionPane.noButtonText String
OptionPane.noIcon Icon
OptionPane.okButtonMnemonic String
OptionPane.okButtonText String
OptionPane.okIcon Icon
OptionPane.questionDialog.border.background Color
OptionPane.questionDialog.titlePane.background Color
OptionPane.questionDialog.titlePane.foreground Color
OptionPane.questionDialog.titlePane.shadow Color
OptionPane.questionIcon Icon
OptionPane.questionSound String
OptionPane.sameSizeButtons Boolean
OptionPane.separatorPadding Integer
OptionPane.setButtonMargin Boolean
OptionPane.titleText String
OptionPane.warningDialog.border.background Color
OptionPane.warningDialog.titlePane.background Color
OptionPane.warningDialog.titlePane.foreground Color
OptionPane.warningDialog.titlePane.shadow Color
OptionPane.warningIcon Icon
OptionPane.warningSound String
OptionPane.windowBindings Object[ ]
OptionPane.yesButtonMnemonic String
OptionPane.yesButtonText String
OptionPane.yesIcon Icon
OptionPaneUI String
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
There is an easy way to change the default font in JOptionPane
. Pass a string modified in html
format, which means you can either use <font>
tag or even CSS.
Using
<font>
tag.
JOptionPane.showMessageDialog(this,
"<html><font face='Calibri' size='15' color='red'>Hello");
Using CSS.
JOptionPane.showMessageDialog(this,
"<html><h1 style='font-family: Calibri; font-size: 36pt;'>Hello");
I have detected that in NIMBUS L&F initially no 'messageFont' is set (UIManager.get("OptionPane.messageFont") == null).
So if you want to derive the new font (/font-size) from the default one you can use the key "OptionPane.font" instead (--> UIManager.get("OptionPane.font")), which apparently never returns null. And then set the derived font using key "OptionPane.messageFont".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With