The key here is to set the implicit output to false Platform.setImplicitExit(false); It is also important to show and hide the scene in a new stream.
Platform.runLater(new Runnable() { @Override public void run() { stage.show(); } }); Platform.runLater(new Runnable() { @Override public void run() { stage.hide(); } });
Next, all the code:
import java.awt.AWTException; import java.awt.MenuItem; import java.awt.PopupMenu; import java.awt.SystemTray; import java.awt.TrayIcon; import java.awt.event.ActionListener; import java.io.IOException; import java.net.URL; import javafx.application.Application; import javafx.application.Platform; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.stage.WindowEvent; import javax.imageio.ImageIO; public class TrayTest extends Application { private boolean firstTime; private TrayIcon trayIcon; public static void main(String[] args) { launch(args); } @Override public void start(Stage stage) throws Exception { createTrayIcon(stage); firstTime = true; Platform.setImplicitExit(false); Scene scene = new Scene(new Group(), 800, 600); stage.setScene(scene); stage.show(); } public void createTrayIcon(final Stage stage) { if (SystemTray.isSupported()) {
alscu source share