Java Swing
Make Java Swing apps feel native on macOS (menu bar, app name, Dock icon, splash screen) and package them with jpackage.
5 min readUpdated
Swing is the desktop UI toolkit that ships with the JDK. Out of the box, a Swing application on macOS keeps its menu bar inside the window and shows “java” as its name in the Dock.
This note collects the system properties and APIs that make a Swing app behave like a native Mac app, two ways to switch panels in a JFrame, a splash screen, and how to build a .dmg with jpackage, including the app icon.
Use the macOS screen menu bar
To move the JMenuBar from the window to the menu bar at the top of the screen, set this system property as a VM option:
-Dapple.laf.useScreenMenuBar=trueBundles in the legacy Apple Java format set it in the Properties dictionary of Info.plist:
<key>Properties</key><dict> <key>apple.laf.useScreenMenuBar</key> <string>true</string> ...</dict>For an app built with jpackage, pass the property with --java-options "-Dapple.laf.useScreenMenuBar=true".
Set the application name
The name shown in the menu bar and the Dock can be set in three ways. Use one of these VM options:
-Xdock:name=YourAppName-Dapple.awt.application.name=YourAppNameOr set the property in code, before any AWT or Swing class is loaded:
System.setProperty("apple.awt.application.name", "YourAppName");Match the window title bar to the system appearance
To make window title bars follow the system’s light or dark appearance, use the VM option:
-Dapple.awt.application.appearance=systemOr set it in code, again before the first window is created:
System.setProperty("apple.awt.application.appearance", "system");Set the Dock icon
For a quick test, set the icon with a VM option:
-Xdock:icon=resources/images/icon.gifIn code, java.awt.Taskbar (JDK 9 and later) sets the Dock icon on macOS, and JFrame.setIconImage sets the window icon on Windows and Linux. This example does both. The image is loaded from the classpath, so adjust the resource path to your project’s layout.
package main;
import javax.swing.JFrame;import javax.swing.JLabel;import java.awt.Image;import java.awt.Taskbar;import java.awt.Toolkit;import java.net.URL;
public final class Main {
public static void main(String[] args) {
final JFrame jFrame = new JFrame();
// Load the image from the classpath final URL imageResource = Main.class.getClassLoader().getResource("resources/images/icon.gif"); final Image image = Toolkit.getDefaultToolkit().getImage(imageResource);
// Dock icon on macOS and other systems that support it if (Taskbar.isTaskbarSupported()) { final Taskbar taskbar = Taskbar.getTaskbar(); if (taskbar.isSupported(Taskbar.Feature.ICON_IMAGE)) { taskbar.setIconImage(image); } else { System.out.println("The OS does not support Taskbar.setIconImage"); } }
// Window icon on Windows and other systems that support it jFrame.setIconImage(image);
// Add some content so the window shows up jFrame.getContentPane().add(new JLabel("Hello World"));
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jFrame.pack(); jFrame.setVisible(true); }}Switch panels from a menu item
There are two ways to replace the content of a JFrame when the user picks a menu item.
The quick way is to remove everything from the content pane and add the new panel. After changing a Swing GUI that is already visible, call revalidate() and repaint() once, after all changes:
JPanel contentPane = (JPanel) frame.getContentPane();contentPane.removeAll();contentPane.add(newPanel);contentPane.revalidate();contentPane.repaint();The better way is a CardLayout: add every panel once, each under a name, and show the one you need with cardLayout.show(container, name). The Oracle tutorial has a complete example.
Show a splash screen
The JVM can show a splash image before your application starts. Name the image with the -splash VM option, or with the SplashScreen-Image entry in the JAR manifest:
-splash:src/main/resources/images/splash.gifIn NetBeans, add the option under Project Properties > Run > VM Options.
Your code can then get the splash screen and change its image while the application loads:
final SplashScreen splash = SplashScreen.getSplashScreen();if (splash == null) { System.out.println("No splash screen: start the JVM with -splash"); return;}try { URL link = URI.create( "https://docs.oracle.com/javase/tutorial/uiswing/examples/misc/SplashDemoProject/src/misc/images/splash.gif") .toURL(); splash.setImageURL(link);} catch (IOException | IllegalStateException ex) { System.out.println("Could not change the splash image: " + ex.getMessage());}SplashScreen.getSplashScreen() returns null when the JVM was started without a splash image. URI.create(...).toURL() replaces the new URL(String) constructor, which is deprecated since Java 20.
Package the app with jpackage
jpackage (JDK 16 and later; an incubator tool in JDK 14 and 15) builds a native installer or app bundle that includes a Java runtime: .dmg or .pkg on macOS, .msi or .exe on Windows, .deb or .rpm on Linux.
Build an executable JAR
Add the maven-jar-plugin to pom.xml so that the JAR’s manifest names the main class. ${exec.mainClass} is the property that NetBeans-generated projects define; replace it with your main class if your pom.xml has no such property.
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.3.0</version> <configuration> <archive> <manifest> <mainClass>${exec.mainClass}</mainClass> </manifest> </archive> </configuration></plugin>Build the JAR:
mvn clean packageThen create the package for the current platform. --jlink-options --bind-services passes --bind-services to jlink, so the bundled runtime also includes modules that provide services your code uses:
jpackage --name YourAppName --input . --main-jar ./target/yourappname-1.0-SNAPSHOT.jar --jlink-options --bind-servicesCreate the macOS app icon
macOS app icons are .icns files. Create one from a 1024 × 1024 PNG with the built-in sips and iconutil tools:
mkdir icons.iconsetsips -z 512 512 icon1024x1024.png --out icons.iconset/icon_512x512.pngcp icon1024x1024.png icons.iconset/icon_512x512@2x.pngsips -z 512 512 icon1024x1024.png --out icons.iconset/icon_256x256@2x.pngsips -z 256 256 icon1024x1024.png --out icons.iconset/icon_256x256.pngsips -z 256 256 icon1024x1024.png --out icons.iconset/icon_128x128@2x.pngsips -z 128 128 icon1024x1024.png --out icons.iconset/icon_128x128.pngsips -z 64 64 icon1024x1024.png --out icons.iconset/icon_32x32@2x.pngsips -z 32 32 icon1024x1024.png --out icons.iconset/icon_32x32.pngsips -z 32 32 icon1024x1024.png --out icons.iconset/icon_16x16@2x.pngsips -z 16 16 icon1024x1024.png --out icons.iconset/icon_16x16.pngiconutil -c icns icons.iconsetrm -R icons.iconsetBuild a .dmg
This command builds a disk image with the icon, version and vendor set:
mvn clean package
jpackage --input target/ \ --name YourAppName \ --main-jar yourappname-1.0-SNAPSHOT.jar \ --main-class com.company.yourappname.Main \ --type dmg \ --icon "src/main/resources/images/icons.icns" \ --app-version "1.0" \ --vendor "YourCompanyName" \ --copyright "Copyright 2023 YourCompanyName" \ --mac-package-name "YourAppName" \ --verbose \ --java-options '--enable-preview'--inputis the directory with the JARs to package;--main-jaris relative to it.--main-classtakes the fully qualified class name, not a path to a.classfile.--java-options '--enable-preview'is only needed if the code uses preview features of the JDK.
Other options you may add, depending on the app:
--module-path <path-to-javafx-sdk>/lib --add-modules javafx.controlsfor an app that uses JavaFX.--mac-sign,--mac-signing-keychainand--mac-signing-key-user-nameto sign the app with your Apple developer certificate.--mac-package-identifierto set the bundle identifier, for examplecom.company.yourappname.
Do not leave commented-out lines inside a command continued with \: the shell ends the command at the comment, and the options after it are lost.