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:

1

2

3

4

5

<dependency>

  <groupId>com.formdev</groupId>

  <artifactId>flatlaf</artifactId>

  <version>3.4</version>

</dependency>

Gradle:

1

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:

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

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.

ADVERTISEMENT

3️⃣ Switching Between Themes

FlatLaf offers four built-in themes:

  • FlatLightLaf
  • FlatDarkLaf
  • FlatIntelliJLaf
  • FlatDarculaLaf

To use a dark theme:

1

UIManager.setLookAndFeel(new FlatDarculaLaf());

Or load IntelliJ’s classic light theme:

1

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:

1

2

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:

1

2

3

4

5

FlatLaf.registerCustomDefaultsSource("themes");

UIManager.setLookAndFeel(

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

);

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

1

2

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:

1

2

3

4

5

<dependency>

  <groupId>com.formdev</groupId>

  <artifactId>flatlaf-extras</artifactId>

  <version>3.4</version>

</dependency>

Example: Installing a custom accent color

1

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

7️⃣ Demo Application

Here’s a minimal example combining several features:

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值