Jan 15th, 2018
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>
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");
-Dapple.awt.application.appearance=system
or
System.setProperty( "apple.awt.application.appearance", "system" );
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);
}
}
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 package
Then use jpackage
for creating executable file.
jpackage --name YourAppName --input . --main-jar ./target/yourappname-1.0-SNAPSHOT.jar --jlink-options --bind-services
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 linesJPanel contentPane = (JPanel) frame.getContentPane();
contentPane.removeAll();
frame.add(new JPanel("New panel"));
contentPane.revalidate();
contentPane.repaint();
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