Please is there another way how to change Font at runtime as using FontUIResource, for the whole AWT/Swing GUI, without any knowledge / interest about if there are local variables and type of JComponents
import java.awt.*;
import java.awt.event.*;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import javax.swing.plaf.basic.BasicComboBoxRenderer;
public class SystemFontDisplayer extends JFrame {
private static final long serialVersionUID = 1L;
private JFrame frame = new JFrame("Nimbus UIDeafaults and Font");
private JComboBox fontsBox;
private javax.swing.Timer timer = null;
private JButton testButton = new JButton("testButton");
private JTextField testTextField = new JTextField("testTextField");
private JLabel testLabel = new JLabel("testLabel");
public SystemFontDisplayer() {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontFamilyNames = ge.getAvailableFontFamilyNames(Locale.getDefault());
fontsBox = new JComboBox(fontFamilyNames);
fontsBox.setSelectedItem(0);
fontsBox.setRenderer(new ComboRenderer());
fontsBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
final String fontName = fontsBox.getSelectedItem().toString();
fontsBox.setFont(new Font(fontName, Font.PLAIN, 16));
start();
}
}
});
fontsBox.setSelectedItem(0);
fontsBox.getEditor().selectAll();
frame.setLayout(new GridLayout(4, 0, 20, 20));
frame.add(fontsBox);
frame.add(testButton);
frame.add(testTextField);
frame.add(testLabel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(200, 105);
frame.pack();
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
fontsBox.setPopupVisible(true);
fontsBox.setPopupVisible(false);
}
});
frame.setVisible(true);
}
private void start() {
timer = new javax.swing.Timer(750, updateCol());
timer.setRepeats(false);
timer.start();
}
public Action updateCol() {
return new AbstractAction("text load action") {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
final Font fnt = new Font(fontsBox.getSelectedItem().toString(), Font.PLAIN, 12);
/*try {
LookAndFeel lnf = UIManager.getLookAndFeel().getClass().newInstance();
final FontUIResource res = new FontUIResource(fnt);
UIDefaults uiDefaults = lnf.getDefaults();
uiDefaults.put("Button.font", res);
uiDefaults.put("TextField.font", res);
uiDefaults.put("Label.font", res);
UIManager.getLookAndFeel().uninitialize();
UIManager.setLookAndFeel(lnf);
} catch (InstantiationException ex) {
Logger.getLogger(SystemFontDisplayer.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(SystemFontDisplayer.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedLookAndFeelException ex) {
Logger.getLogger(SystemFontDisplayer.class.getName()).log(Level.SEVERE, null, ex);
}
UIDefaults defaults = UIManager.getDefaults();
final FontUIResource res = new FontUIResource(fnt);
defaults.put("Button.font", res);
defaults.put("TextField.font", res);
defaults.put("Label.font", res);
SwingUtilities.updateComponentTreeUI(frame);*/
final FontUIResource res = new FontUIResource(fnt);
UIManager.getLookAndFeelDefaults().put("Button.font", res);
UIManager.getLookAndFeelDefaults().put("TextField.font", res);
UIManager.getLookAndFeelDefaults().put("Label.font", res);
SwingUtilities.updateComponentTreeUI(frame);
}
};
}
public static void main(String arg[]) {
/*try {
for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(laf.getName())) {
UIManager.setLookAndFeel(laf.getClassName());
}
}
} catch (Exception e) {
e.printStackTrace();
}*/
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
SystemFontDisplayer systemFontDisplayer = new SystemFontDisplayer();
}
});
}
private class ComboRenderer extends BasicComboBoxRenderer {
private static final long serialVersionUID = 1L;
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
final Object fntObj = value;
final String fontFamilyName = (String) fntObj;
setFont(new Font(fontFamilyName, Font.PLAIN, 16));
return this;
}
}
}
.
EDIT: I posted narrative with Nimbus, then code for Nimbus
import java.awt.*;
import java.awt.event.*;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import javax.swing.plaf.basic.BasicComboBoxRenderer;
public class SystemFontDisplayer extends JFrame {
private static final long serialVersionUID = 1L;
private JFrame frame = new JFrame("Nimbus UIDeafaults and Font");
private JComboBox fontsBox;
private javax.swing.Timer timer = null;
private JButton testButton = new JButton("testButton");
private JTextField testTextField = new JTextField("testTextField");
private JLabel testLabel = new JLabel("testLabel");
public SystemFontDisplayer() {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontFamilyNames = ge.getAvailableFontFamilyNames(Locale.getDefault());
fontsBox = new JComboBox(fontFamilyNames);
fontsBox.setSelectedItem(0);
fontsBox.setRenderer(new ComboRenderer());
fontsBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
final String fontName = fontsBox.getSelectedItem().toString();
fontsBox.setFont(new Font(fontName, Font.PLAIN, 16));
start();
}
}
});
fontsBox.setSelectedItem(0);
fontsBox.getEditor().selectAll();
frame.setLayout(new GridLayout(4, 0, 20, 20));
frame.add(fontsBox);
frame.add(testButton);
frame.add(testTextField);
frame.add(testLabel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(200, 105);
frame.pack();
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
fontsBox.setPopupVisible(true);
fontsBox.setPopupVisible(false);
}
});
frame.setVisible(true);
}
private void start() {
timer = new javax.swing.Timer(750, updateCol());
timer.setRepeats(false);
timer.start();
}
public Action updateCol() {
return new AbstractAction("text load action") {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
final Font fnt = new Font(fontsBox.getSelectedItem().toString(), Font.PLAIN, 12);
try {
LookAndFeel lnf = UIManager.getLookAndFeel().getClass().newInstance();
final FontUIResource res = new FontUIResource(fnt);
UIDefaults uiDefaults = lnf.getDefaults();
uiDefaults.put("Button.font", res);
uiDefaults.put("TextField.font", res);
uiDefaults.put("Label.font", res);
UIManager.getLookAndFeel().uninitialize();
UIManager.setLookAndFeel(lnf);
} catch (InstantiationException ex) {
Logger.getLogger(SystemFontDisplayer.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(SystemFontDisplayer.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedLookAndFeelException ex) {
Logger.getLogger(SystemFontDisplayer.class.getName()).log(Level.SEVERE, null, ex);
}
UIDefaults defaults = UIManager.getDefaults();
final FontUIResource res = new FontUIResource(fnt);
defaults.put("Button.font", res);
defaults.put("TextField.font", res);
defaults.put("Label.font", res);
SwingUtilities.updateComponentTreeUI(frame);
/*final FontUIResource res = new FontUIResource(fnt);
UIManager.getLookAndFeelDefaults().put("Button.font", res);
UIManager.getLookAndFeelDefaults().put("TextField.font", res);
UIManager.getLookAndFeelDefaults().put("Label.font", res);
SwingUtilities.updateComponentTreeUI(frame);*/
}
};
}
public static void main(String arg[]) {
try {
for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(laf.getName())) {
UIManager.setLookAndFeel(laf.getClassName());
}
}
} catch (Exception e) {
e.printStackTrace();
}
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
SystemFontDisplayer systemFontDisplayer = new SystemFontDisplayer();
}
});
}
private class ComboRenderer extends BasicComboBoxRenderer {
private static final long serialVersionUID = 1L;
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
final Object fntObj = value;
final String fontFamilyName = (String) fntObj;
setFont(new Font(fontFamilyName, Font.PLAIN, 16));
return this;
}
}
}
FontShower
. – Crowfootjavax.swing.Timer
; only need onesetSelectedItem()
; considergetAvailableFontFamilyNames(Locale.getDefault())
. Works on Mac OS. – CrowfootmanItemInCombo()
looks handy, too. – Crowfoot