Over a million developers have joined DZone.

On the Fly Font Size Switching on the Java Desktop

DZone's Guide to

On the Fly Font Size Switching on the Java Desktop

· Java Zone
Free Resource

Just released, a free O’Reilly book on Reactive Microsystems: The Evolution of Microservices at Scale. Brought to you in partnership with Lightbend.

Let's create a font size switcher enabling the user to select a new font size at runtime in our NetBeans Platform based Java desktop app. Below you see the result of the explanation that follows, i.e., notice the "Font Size" label in the toolbar, next to the JList. When a new size is selected and the user double-clicks on the JList, the new font size is written into the app's configuration file, the app closes, restarts, at which point the new font size is applied, and the current font size is set in the JList when the app has restarted.

The place where the font size is set is via "--fontsize" in the app's ".conf" file, which is located in the "etc" folder in the app's installation directory. Below you learn how to set "--fontsize" programmatically.

What you see here is the constructor of the FontSwitcherPanel, which is put into the toolbar via an AbstractAction that implements Presenter.Toolbar:

public class FontSwitcherPanel extends javax.swing.JPanel {

public FontSwitcherPanel() {
String storedFont = NbPreferences.forModule(FontSwitcherPanel.class).get("selectedFont", "11");
jList1.setSelectedValue(storedFont, true);
MouseListener mouseListener = new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
String selectedFont = (String) jList1.getSelectedValue();
NbPreferences.forModule(FontSwitcherPanel.class).put("selectedFont", selectedFont);
File f = InstalledFileLocator.getDefault().locate("../etc/wordeditor.conf", "org.font.switcher", false);
FileObject fo = FileUtil.toFileObject(f);
Properties prop = new Properties();
try {
} catch (IOException ex) {
prop.setProperty("default_options", "--branding wordeditor -J-Xms24m -J-Xmx64m --fontsize " + selectedFont);
try {
prop.store(new FileOutputStream(f), null);
} catch (IOException ex) {


So, we start by looking for a Preference named "selectedFont". If it doesn't exist, we assume that 11 (the NetBeans Platform font size default) is used. Next, we set the JList to the font size (either the found font size or our default).

Then there's a MouseListener on the JList. When a double-click is performed, we create the "selectedFont" preference and set it, based on the currently selected font size. Next we use the Module System API's "InstalledFileLocator" class to find the ".conf" file. Hence, note that this code will not work during development. Instead, you need to create a ZIP distribution and unzip it, then run the app from the folder where you unzipped it, which will at that stage have a ".conf" file.

Having found our ".conf" file, we identify the property where the "--fontsize" is set, which we write into the ".conf" file. Finally, we mark the app to be restarted once it has been closed, after which we close the app. Then the app restarts, with the new font size applied, since the changed ".conf" file is used when the app restarts.

The above is a nice feature when you're working with accessibility requirements for your app.


Strategies and techniques for building scalable and resilient microservices to refactor a monolithic application step-by-step, a free O'Reilly book. Brought to you in partnership with Lightbend.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}