References
Use -Dapple.laf.useScreenMenuBar
as VM option: -Dapple.laf.useScreenMenuBar=true
Alternatively, set the property in your application bundle’s Info.plist
.
Application/MyApp/Info.plist
1
2
3
4
5
6
|
<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:
1
|
System.setProperty("apple.awt.application.name", "YourAppName");
|
Appearance of window title bars
-Dapple.awt.application.appearance=system
or
1
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
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
.
1
2
3
4
5
6
7
8
9
10
11
12
|
<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:
Then use jpackage
for creating executable file.
1
|
jpackage --name YourAppName --input . --main-jar ./target/yourappname-1.0-SNAPSHOT.jar --jlink-options --bind-services
|
- 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
1
2
3
4
5
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
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'
|