Skip to content

Swing

Java Swing on macOS: screen menu bar, app name and dock icon, splash screens and executables.

3 min read

References

Screen menu bar

Use -Dapple.laf.useScreenMenuBar as VM option: -Dapple.laf.useScreenMenuBar=true

Alternatively, set the property in your application bundle’s Info.plist.

<key>Properties</key>
<dict>
<key>apple.laf.useScreenMenuBar</key>
<string>true</string>
...
</dict>

Application name

Use -Xdock:name as VM option: -Xdock:name=YourAppName

or

Use -Dapple.awt.application.name as VM option: -Dapple.awt.application.name=YourAppName

or

Set the system property like:

System.setProperty("apple.awt.application.name", "YourAppName");

Appearance of window title bars

-Dapple.awt.application.appearance=system

or

System.setProperty( "apple.awt.application.appearance", "system" );

How to set dock icon of Java application

Use -Xdock:icon as VM option: -Xdock:icon=resources/images/icon.gif

or

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();
//loading an image from a file
final Toolkit defaultToolkit = Toolkit.getDefaultToolkit();
final URL imageResource = Main.class.getClassLoader().getResource("resources/images/icon.gif");
final Image image = defaultToolkit.getImage(imageResource);
//this is new since JDK 9
final Taskbar taskbar = Taskbar.getTaskbar();
try {
//set icon for mac os (and other systems which do support this method)
taskbar.setIconImage(image);
} catch (final UnsupportedOperationException e) {
System.out.println("The os does not support: 'taskbar.setIconImage'");
} catch (final SecurityException e) {
System.out.println("There was a security exception for: 'taskbar.setIconImage'");
}
//set icon for windows os (and other systems which do support this method)
jFrame.setIconImage(image);
//adding something to the window so it does show up
jFrame.getContentPane().add(new JLabel("Hello World"));
//some default JFrame things
jFrame.setDefaultCloseOperation(jFrame.EXIT_ON_CLOSE);
jFrame.pack();
jFrame.setVisible(true);
}
}

Create executable file

Add maven-jar-plugin to the pom.xml.

<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>

Now create .jar file:

Terminal window
mvn clean package

Then use jpackage for creating executable file.

Terminal window
jpackage --name YourAppName --input . --main-jar ./target/yourappname-1.0-SNAPSHOT.jar --jlink-options --bind-services

Switching JPanels with MenuItem inside JFrame

  1. Remove (JFrame.getContentPane.removeAll()) and add JPanel to JFrame, required to call JFrame.(re)validate and JFrame.repaint after all changes to already visible Swing GUI is done, once time, last code lines
JPanel contentPane = (JPanel) frame.getContentPane();
contentPane.removeAll();
frame.add(new JPanel("New panel"));
contentPane.revalidate();
contentPane.repaint();
  1. (better, correct, proper of ways) Use CardLayout, code example in official Oracle tutorial, a few good, some excelent examples here.

Splash Screen

final SplashScreen splash = SplashScreen.getSplashScreen();
if (splash == null) {
System.out.println("SplashScreen.getSplashScreen() returned null");
return;
}
URL link;
try {
link =
new URL(
"http://docs.oracle.com/javase/tutorial/uiswing/examples/misc/SplashDemoProject/src/misc/images/splash.gif");
} catch (MalformedURLException ex) {
System.out.println("MalformedURLException link:77");
return;
}
try {
splash.setImageURL(link);
} catch (NullPointerException | IOException | IllegalStateException ex) {
System.out.println("NullPointer or IO or IllegalState setImageUrl:85");
return;
}

To get it to work in Netbeans; Project Properties >> Run >> VM Options >> add -splash:src/main/resources/images/splash.gif

MacOS Application Icons

Terminal window
mkdir icons.iconset
sips -z 512 512 icon1024x1024.png --out icons.iconset/icon_512x512.png
cp icon1024x1024.png icons.iconset/icon_512x512@2x.png
sips -z 512 512 icon1024x1024.png --out icons.iconset/icon_256x256@2x.png
sips -z 256 256 icon1024x1024.png --out icons.iconset/icon_256x256.png
sips -z 256 256 icon1024x1024.png --out icons.iconset/icon_128x128@2x.png
sips -z 128 128 icon1024x1024.png --out icons.iconset/icon_128x128.png
sips -z 64 64 icon1024x1024.png --out icons.iconset/icon_32x32@2x.png
sips -z 32 32 icon1024x1024.png --out icons.iconset/icon_32x32.png
sips -z 32 32 icon1024x1024.png --out icons.iconset/icon_16x16@2x.png
sips -z 16 16 icon1024x1024.png --out icons.iconset/icon_16x16.png
iconutil -c icns icons.iconset
rm -R icons.iconset
Terminal window
mvn clean package
jpackage --name YourAppName --input . --main-jar target/yourappname-1.0-SNAPSHOT.jar --jlink-options --bind-services
jpackage --input target/ \
--name YourAppName \
--input . \
--main-jar target/yourappname-1.0-SNAPSHOT.jar \
--main-class src/main/java/com/company/yourappname/Main.class \
--type dmg \
--icon "src/main/resources/images/icons.icns" \
--app-version "1.0" \
--vendor "ARI Systems" \
--copyright "Copyright 2023 YourCompanyName" \
--mac-package-name "YourAppName" \
#--module-path "/opt/javafx/javafx-sdk-14.0.1/lib" \
#--add-modules javafx.controls \
#--mac-sign \
#--mac-bundle-identifier "MyAppName-1.2.3" \
#--mac-bundle-name "My App Name" \
#--mac-bundle-signing-prefix "" \
#--mac-signing-keychain "" \
#--mac-signing-key-user-name "" \
--verbose \
--java-options '--enable-preview'

Related articles

3 min read

Jakarta

Jakarta EE namespaces, project settings and what changed in Jakarta EE 10 and 11.

  • Java & Jakarta EE
8 min read

JSF

Jakarta Faces (JSF) notes: WebSocket push, scopes, events, Ajax, security and cross-field validation.

  • Java & Jakarta EE
  • Web & Frontend
2 min read

Jasper Reports

JasperReports tips: internationalization, pagination, split types and floating elements.

  • Java & Jakarta EE

Type to search the whole site.