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:
mvn clean packageThen use jpackage for creating executable file.
jpackage --name YourAppName --input . --main-jar ./target/yourappname-1.0-SNAPSHOT.jar --jlink-options --bind-servicesSwitching JPanels with MenuItem inside JFrame
- Remove (
JFrame.getContentPane.removeAll()) and addJPaneltoJFrame, required to callJFrame.(re)validateandJFrame.repaintafter 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();- (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
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.iconsetmvn 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'