Core Java

Modern Look and Feel in Java Swing: FlatLaf Deep Dive

How to Refresh Old Swing UIs with a Clean, Modern Aesthetic

Java Swing has been powering desktop applications for over two decades. While Swing is incredibly capable and stable, its default look and feel has aged—often making apps look like relics of the early 2000s.

FlatLaf is a modern, open-source look and feel that breathes new life into Swing UIs, offering:

✅ A flat, clean design inspired by IntelliJ IDEA and modern UI trends
✅ Dark and light themes out of the box
✅ High-DPI scaling support
✅ Easy customization

In this article, you’ll learn how to:

  • Install FlatLaf in a Swing project
  • Switch between themes
  • Tweak the appearance with properties and custom colors
  • Build a professional-looking desktop app in minutes

Let’s get started!

Why Use FlatLaf?

FlatLaf stands out among Swing look and feels because:

  • It supports Windows, macOS, and Linux consistently
  • It’s actively maintained
  • It’s designed to look modern without heavy custom drawing code
  • It integrates with popular Swing frameworks and components

If you’ve been stuck with Metal, Nimbus, or legacy third-party themes, FlatLaf can dramatically improve your app’s visual appeal.

1️⃣ Setting Up FlatLaf

FlatLaf is published to Maven Central, making it simple to integrate.

Maven Dependency:

<dependency>
  <groupId>com.formdev</groupId>
  <artifactId>flatlaf</artifactId>
  <version>3.4</version>
</dependency>

Gradle:

implementation 'com.formdev:flatlaf:3.4'

2️⃣ Applying FlatLaf in Your Application

You can install FlatLaf globally in your main method before creating any Swing components.

Example:

import com.formdev.flatlaf.FlatLightLaf;

public class FlatLafDemo {
    public static void main(String[] args) {
        // Set FlatLaf look and feel
        try {
            UIManager.setLookAndFeel(new FlatLightLaf());
        } catch (UnsupportedLookAndFeelException ex) {
            System.err.println("Failed to initialize LaF");
        }

        // Create and show your UI
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("FlatLaf Demo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new JLabel("Hello, modern Swing!"));
            frame.setSize(300, 200);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        });
    }
}

Launch this example, and you’ll immediately see the flat, modern styling applied.

3️⃣ Switching Between Themes

FlatLaf offers four built-in themes:

  • FlatLightLaf
  • FlatDarkLaf
  • FlatIntelliJLaf
  • FlatDarculaLaf

To use a dark theme:

UIManager.setLookAndFeel(new FlatDarculaLaf());

Or load IntelliJ’s classic light theme:

UIManager.setLookAndFeel(new FlatIntelliJLaf());

4️⃣ High-DPI Support

FlatLaf automatically scales components on HiDPI screens, so text and icons look crisp on Retina and 4K displays. You don’t need extra configuration—just ensure your Java runtime is up to date (Java 11+ recommended).

5️⃣ Customizing the Look and Feel

FlatLaf provides several ways to tweak styles:

Setting UI Defaults

For example, change button arc radius or focus width:

UIManager.put("Button.arc", 999); // Fully round buttons
UIManager.put("Component.focusWidth", 2);

Loading Custom Themes

FlatLaf supports .properties theme files to override colors, fonts, and styles.

Example:

FlatLaf.registerCustomDefaultsSource("themes");

UIManager.setLookAndFeel(
    new FlatLaf.ThemeLaf("MyCustomTheme", "themes/myTheme.properties")
);

In resources/themes/myTheme.properties, you can override specific keys:

Button.background = #3498db
Button.foreground = #ffffff

6️⃣ FlatLaf Extras

FlatLaf Extras is an optional library providing:

  • Animated transitions
  • Accent color support
  • Custom font configurations

Maven:

<dependency>
  <groupId>com.formdev</groupId>
  <artifactId>flatlaf-extras</artifactId>
  <version>3.4</version>
</dependency>

Example: Installing a custom accent color

FlatLaf.setPreferredAccentColor(new Color(0xE67E22)); // Orange accent

7️⃣ Demo Application

Here’s a minimal example combining several features:

public class FlatLafModernUI {
    public static void main(String[] args) {
        FlatDarculaLaf.setup();

        UIManager.put("Button.arc", 20);

        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("Modern FlatLaf UI");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new FlowLayout());

            JButton button = new JButton("Click Me");
            JTextField textField = new JTextField("Type here...", 15);
            JCheckBox checkBox = new JCheckBox("Enable Option");

            frame.add(button);
            frame.add(textField);
            frame.add(checkBox);

            frame.setSize(400, 150);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        });
    }
}

Run this code and see a sleek, dark-themed UI.

8️⃣ Tips for Best Results

Set the look and feel before creating any Swing components
Use consistent font sizes across your app
Test on different platforms (Windows, macOS, Linux)
Combine FlatLaf with modern icons (e.g., Material Icons)
Avoid mixing FlatLaf with unsupported third-party look and feels

Conclusion

With FlatLaf, you don’t have to abandon Swing to achieve a modern look and feel. You can refresh your applications quickly and provide a clean, professional user experience that feels up to date.

Explore the official project for even more customization options and examples.

Resources

Eleftheria Drosopoulou

Eleftheria is an Experienced Business Analyst with a robust background in the computer software industry. Proficient in Computer Software Training, Digital Marketing, HTML Scripting, and Microsoft Office, they bring a wealth of technical skills to the table. Additionally, she has a love for writing articles on various tech subjects, showcasing a talent for translating complex concepts into accessible content.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Back to top button