I was wondering how to set the default font for my entire Java swing program. From my research it appears it can be done with UIManager
, something to do with LookAndFeel
, but I can't find specifically how to do it, and the UIManager
appears pretty complicated.
try:
public static void setUIFont (javax.swing.plaf.FontUIResource f){
java.util.Enumeration keys = UIManager.getDefaults().keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
Object value = UIManager.get (key);
if (value instanceof javax.swing.plaf.FontUIResource)
UIManager.put (key, f);
}
}
Call by ...
setUIFont (new javax.swing.plaf.FontUIResource("Serif",Font.ITALIC,12));
UIManager.getLookAndFeelDefaults()
instead of UIManager.getDefaults()
and using the returned reference instead of UIManager.put()
(see my answer below). It works only when overriding the default L&F though (Nimbus in my case)... –
Gunning UIManager.put("Button.font", /* font of your liking */);
UIManager.put("ToggleButton.font", /* font of your liking */);
UIManager.put("RadioButton.font", /* font of your liking */);
UIManager.put("CheckBox.font", /* font of your liking */);
UIManager.put("ColorChooser.font", /* font of your liking */);
UIManager.put("ComboBox.font", /* font of your liking */);
UIManager.put("Label.font", /* font of your liking */);
UIManager.put("List.font", /* font of your liking */);
UIManager.put("MenuBar.font", /* font of your liking */);
UIManager.put("MenuItem.font", /* font of your liking */);
UIManager.put("RadioButtonMenuItem.font", /* font of your liking */);
UIManager.put("CheckBoxMenuItem.font", /* font of your liking */);
UIManager.put("Menu.font", /* font of your liking */);
UIManager.put("PopupMenu.font", /* font of your liking */);
UIManager.put("OptionPane.font", /* font of your liking */);
UIManager.put("Panel.font", /* font of your liking */);
UIManager.put("ProgressBar.font", /* font of your liking */);
UIManager.put("ScrollPane.font", /* font of your liking */);
UIManager.put("Viewport.font", /* font of your liking */);
UIManager.put("TabbedPane.font", /* font of your liking */);
UIManager.put("Table.font", /* font of your liking */);
UIManager.put("TableHeader.font", /* font of your liking */);
UIManager.put("TextField.font", /* font of your liking */);
UIManager.put("PasswordField.font", /* font of your liking */);
UIManager.put("TextArea.font", /* font of your liking */);
UIManager.put("TextPane.font", /* font of your liking */);
UIManager.put("EditorPane.font", /* font of your liking */);
UIManager.put("TitledBorder.font", /* font of your liking */);
UIManager.put("ToolBar.font", /* font of your liking */);
UIManager.put("ToolTip.font", /* font of your liking */);
UIManager.put("Tree.font", /* font of your liking */);
UIManager.put("OptionPane.messageFont", /*font*/);
and UIManager.put("OptionPane.buttonFont", /*font*/);
–
Engvall SwingUtilities.updateComponentTreeUI(this);
–
Wrongdoer java -Dswing.aatext=true -Dswing.plaf.metal.controlFont=Tahoma -Dswing.plaf.metal.userFont=Tahoma …
This will not only set Tahoma on your complete UI but also turn on anti-aliasing which makes any font much more beautiful immediately.
I think this is better, calling it for the current laf instead of the whole UIManager put this
UIManager.getLookAndFeelDefaults()
.put("defaultFont", new Font("Arial", Font.BOLD, 14));
Somewhere in the main before instantiating your JFrame object. It worked perfectly for me. Remember this is the default font, for the components that have no specified font.
source: http://www.java.net/node/680725
Inspired by Romain Hippeau, use this code if you want to set just the font size.
for (Map.Entry<Object, Object> entry : javax.swing.UIManager.getDefaults().entrySet()) {
Object key = entry.getKey();
Object value = javax.swing.UIManager.get(key);
if (value != null && value instanceof javax.swing.plaf.FontUIResource) {
javax.swing.plaf.FontUIResource fr=(javax.swing.plaf.FontUIResource)value;
javax.swing.plaf.FontUIResource f = new javax.swing.plaf.FontUIResource(fr.getFamily(), fr.getStyle(), FONT_SIZE);
javax.swing.UIManager.put(key, f);
}
}
UIManager.put(key, new javax.swing.plaf.FontUIResource(fr.deriveFont(fr.getSize2D() * 2.0f)));
–
Radiothermy Be aware that the way to set the default font depends on the Look And Feel you're using. The solution described by Romain Hippeau works fine with a lot of LAF but not with Nimbus. The one posted by sherif works fine with Nimbus, but not with others (Metal, for instance).
Combining both could work on most of LAF, but none of these solutions works with GTK+ LAF.
I think (but I'm not sure), there's no cross-platform solution.
As a completion of @Amir answer, this is the complete list of keys
I use this function
private void setFont(FontUIResource myFont) {
UIManager.put("CheckBoxMenuItem.acceleratorFont", myFont);
UIManager.put("Button.font", myFont);
UIManager.put("ToggleButton.font", myFont);
UIManager.put("RadioButton.font", myFont);
UIManager.put("CheckBox.font", myFont);
UIManager.put("ColorChooser.font", myFont);
UIManager.put("ComboBox.font", myFont);
UIManager.put("Label.font", myFont);
UIManager.put("List.font", myFont);
UIManager.put("MenuBar.font", myFont);
UIManager.put("Menu.acceleratorFont", myFont);
UIManager.put("RadioButtonMenuItem.acceleratorFont", myFont);
UIManager.put("MenuItem.acceleratorFont", myFont);
UIManager.put("MenuItem.font", myFont);
UIManager.put("RadioButtonMenuItem.font", myFont);
UIManager.put("CheckBoxMenuItem.font", myFont);
UIManager.put("OptionPane.buttonFont", myFont);
UIManager.put("OptionPane.messageFont", myFont);
UIManager.put("Menu.font", myFont);
UIManager.put("PopupMenu.font", myFont);
UIManager.put("OptionPane.font", myFont);
UIManager.put("Panel.font", myFont);
UIManager.put("ProgressBar.font", myFont);
UIManager.put("ScrollPane.font", myFont);
UIManager.put("Viewport.font", myFont);
UIManager.put("TabbedPane.font", myFont);
UIManager.put("Slider.font", myFont);
UIManager.put("Table.font", myFont);
UIManager.put("TableHeader.font", myFont);
UIManager.put("TextField.font", myFont);
UIManager.put("Spinner.font", myFont);
UIManager.put("PasswordField.font", myFont);
UIManager.put("TextArea.font", myFont);
UIManager.put("TextPane.font", myFont);
UIManager.put("EditorPane.font", myFont);
UIManager.put("TabbedPane.smallFont", myFont);
UIManager.put("TitledBorder.font", myFont);
UIManager.put("ToolBar.font", myFont);
UIManager.put("ToolTip.font", myFont);
UIManager.put("Tree.font", myFont);
UIManager.put("FormattedTextField.font", myFont);
UIManager.put("IconButton.font", myFont);
UIManager.put("InternalFrame.optionDialogTitleFont", myFont);
UIManager.put("InternalFrame.paletteTitleFont", myFont);
UIManager.put("InternalFrame.titleFont", myFont);
}
and i call it in main
before invoking the ui
setFont(new FontUIResource(new Font("Cabin", Font.PLAIN, 14)));
For a complete list of Swing UI Manager keys check this link
The correct answer is the one given by Amir Raminfar but you have to encapsulate the font as a FontUIResource.
For example:
UIManager.put("Button.font", new FontUIResource(new Font ("Helvetica", Font.BOLD, 16)));
I'm using Nimbus L&F.
Using code from @Romain Hippeau, I had to use UIManager.getLookAndFeelDefaults()
instead of UIManager.getDefaults()
and use the returned reference to put
modified values:
int szIncr = 5; // Value to increase the size by
UIDefaults uidef = UIManager.getLookAndFeelDefaults();
for (Entry<Object,Object> e : uidef.entrySet()) {
Object val = e.getValue();
if (val != null && val instanceof FontUIResource) {
FontUIResource fui = (FontUIResource)val;
uidef.put(e.getKey(), new FontUIResource(fui.getName(), fui.getStyle(), fui.getSize()+szIncr));
}
}
For some reason, it does not seem to work with the default L&F... (based on the limited tests I performed)
To solve this problem, I just implement AWTEventListener and listen for COMPONENT_ADDED of ContainerEvent.
All story description at: http://wiki.idempiere.org/en/Swing_Miss_Support_Some_Language
- Implement AWTEventListener
public void eventDispatched(AWTEvent event) {
if (!isMissSupportGlyph || !(event instanceof ComponentEvent) || !(event instanceof ContainerEvent))
return;
if (event instanceof ContainerEvent){
ContainerEvent containerEvent = (ContainerEvent)event;
if (containerEvent.getID() == ContainerEvent.COMPONENT_ADDED){
updateChildControlFont(containerEvent.getChild());
}
}
}
- Add registry listener (the best place to run this is when starting the program)
Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.COMPONENT_EVENT_MASK | AWTEvent.CONTAINER_EVENT_MASK);
I used the Synth look and feel XML file and defined the fonts there. Easy, flexible and continent.
You need to create a class with a createFont
like:
public class CustomFontResource {
public static FontUIResource createFont(String path, final int size) throws IOException, FontFormatException {
Font font = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream(path));
return new FontUIResource(font.deriveFont(Font.PLAIN, size));
}
And in your synth xml define the font like:
<object id="Basic_Regular" class="<your CustomFontResource class>"
method="createFont">
<string>path_to_your_font</string>
<int>font_size</int>
</object>
then you may use it as a reference wherever you want in the xml.
None of these solutions work fine for me, I built my own (stupid) one but it works:
private void changeFontRecursive(Container root, Font font) {
for (Component c : root.getComponents()) {
c.setFont(font);
if (c instanceof Container) {
changeFontRecursive((Container) c, font);
}
}
}
© 2022 - 2024 — McMap. All rights reserved.
Related
with 3-5 excelent threads about similair issue – Pitre