DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > On the Fly Font Size Switching on the Java Desktop

On the Fly Font Size Switching on the Java Desktop

Geertjan Wielenga user avatar by
Geertjan Wielenga
·
Apr. 11, 11 · Java Zone · News
Like (0)
Save
Tweet
5.51K Views

Join the DZone community and get the full member experience.

Join For Free

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() {
initComponents();
String storedFont = NbPreferences.forModule(FontSwitcherPanel.class).get("selectedFont", "11");
jList1.setSelectedValue(storedFont, true);
MouseListener mouseListener = new MouseAdapter() {
@Override
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 {
prop.load(fo.getInputStream());
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
prop.setProperty("default_options", "--branding wordeditor -J-Xms24m -J-Xmx64m --fontsize " + selectedFont);
try {
prop.store(new FileOutputStream(f), null);
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
LifecycleManager.getDefault().markForRestart();
LifecycleManager.getDefault().exit();
}
}
};
jList1.addMouseListener(mouseListener);
}

...
...
...

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.

 

app Desktop (word processor) Java (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How BDD Works Well With EDA
  • Creating a REST Web Service With Java and Spring (Part 1)
  • Instancio: Test Data Generator for Java (Part 2)
  • Why Is Software Integration Important for Business?

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo