Java: Changing user interface fonts (Nimbus) doesn't work!

I refer to this link to Nimbus .

I tried to set the global font a bit more:

UIManager.put("defaultFont", new Font(Font.SANS_SERIF, 0, 16)); 

... only works for menus, but nothing else (buttons, labels).

I tried changing the shortcuts and buttons with

 UIManager.put("Button.font", new Font(Font.SANS_SERIF, 0, 16)); UIManager.put("Label.font", new Font(Font.SANS_SERIF, 0, 16)); 

but the font remains .

The only thing that worked for me was the font output :

 someButton.setFont(someButton.getFont().deriveFont(16f)); 

But this is not an option, since it needs to be done manually for each element.

Note that getting a font for UIManager does not work :

 UIManager.put("Label.font", UIManager.getFont("Label.font").deriveFont(16f)); 

I tested everything under Linux and Windows: the same behavior.

I just don’t understand how the API can be so confusing. If the setFont (..) method is called, then I expect it to set the font. If this method does not install the font in any imaginable circumstances, then it should be obsolete.

EDIT:
The problem is not only applicable to Nimbus, but also to LAF by default.

+6
java user-interface fonts swing nimbus
source share
6 answers

This one works with JDK6 and JDK7. Copy + paste and have fun;)

Note: for JDK6, change
javax.swing.plaf.nimbus to
com.​sun.​java.​swing.​plaf.​nimbus .

the code

 import java.awt.*; import java.lang.reflect.*; import javax.swing.*; import javax.swing.plaf.nimbus.*; public class Main { public static void main(String[] args) throws InterruptedException, InvocationTargetException { SwingUtilities.invokeAndWait(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(new NimbusLookAndFeel() { @Override public UIDefaults getDefaults() { UIDefaults ret = super.getDefaults(); ret.put("defaultFont", new Font(Font.MONOSPACED, Font.BOLD, 16)); // supersize me return ret; } }); new JFrame("Hello") { { setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(new FlowLayout(FlowLayout.LEFT)); setSize(500, 500); setLocationRelativeTo(null); add(new JLabel("someLabel 1")); add(new JButton("someButton 1")); add(new JLabel("someLabel 2")); add(new JButton("someButton 2")); setVisible(true); } }; } catch (Exception ex) { throw new Error(ex); } } }); } } 
+8
source share

By default, nimbus values ​​are created with laziness, so setting "defaultFont" before painting the screen will add the font to the parent defaults, and not to the default nimbus values.

Workaround: Forcibly nimbus initializes default values ​​and sets default values:

 NimbusLookAndFeel laf = new NimbusLookAndFeel(); UIManager.setLookAndFeel(laf); laf.getDefaults().put("defaultFont", new Font("Monospaced", Font.BOLD, 12)); 

Note: this code is more efficient than overriding getDefaults (), as suggested above.

+4
source share

One thing that amazes me to this day is that the LaF settings [setFont, setBackground, etc.] do not actually set real properties. The spectrum says LaFs can ignore custom fonts, colors, etc. This is why GTKLaF is completely broken. It uses the gtk system parameter settings, not the programmer settings. IIRC Nimbus has a separate batch class that contains default values ​​(NimbusDefaults?) And is not accessible.

I suggest you never use GTK or Nimbus LAF if you plan to customize the look in any way.

A quick google search includes this for GTK

A discussion of these issues in nimbus can be found here .

+1
source share

Wrap your font with FontUIResource . I had the same problem with UIManager colors, and ColorUIResource fixed everything. Without digging through the JDK, I think there are some places where components expect (read: check through instanceof) for UIResources (maybe someone can confirm this)

+1
source share

The answer is in one line of code (if you have already installed Nimbus LaF):

UIManager.getLookAndFeelDefaults().put("defaultFont", new Font(Font.SANS_SERIF, 0, 20));

Of course, you need to call this before creating any GUI components, i.e. right in your main right after installing Nimbus LaF.

+1
source share

The Java LAF API is a bit awkward, but there is nothing better than checking the source code for answers.

Please note that MetalLookAndFeel and Nimbus are different implementations, and the properties for each of them do not have to be the same.

The following examples use MetalLookAndFeel.

 package com.stackoverflow.laf.font; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.SwingUtilities; import javax.swing.UIManager; public class SetFontExample { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { UIManager.put("Label.font", new Font(Font.SANS_SERIF, 0, 20)); try { UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); } catch (Exception e) { e.printStackTrace(); } JFrame frame = new JFrame("Set font example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new JLabel("Font test")); frame.pack(); frame.setVisible(true); } }); } } 

This works because the "Label.font" property exists on Metal, and it uses this property correctly.

You can check it like this:

 package com.stackoverflow.laf; import javax.swing.SwingUtilities; import javax.swing.UIDefaults; import javax.swing.UIManager; public class ListLAFUIDefaults { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { try { // Choose LAF UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); } catch (Exception e) { e.printStackTrace(); } UIDefaults defaults = UIManager.getLookAndFeel().getDefaults(); System.out.println(defaults); // Check a property String propertyKey = "defaultFont"; System.out.println(UIManager.getLookAndFeel().getName() + (defaults.containsKey(propertyKey) ? " contains " : " doesn't contain ") + "property " + propertyKey); } }); } } 
0
source share

All Articles