I have a java application running java http server. This java application should run continuously. I don't want to open javafx gui when the program is run for the first time.
As I said, the application should run continuously. The user should be able to open the user interface at any time by clicking on the system tray icon. Or should be able to close the cross-button in the interface.
I used Platform.setImplicitExit (false)
to not stop the java application from pressing the cross-button on the interface.
If the user wants to see the screen again, I want to re-render the screen by pressing the system tray.
I want to show and hide the user interface without closing the java program. What is best practice I'm waiting for your help.
Related codes are below.
public class Gui extends Application {
@Override
public void start(Stage stage) throws Exception {
Platform.setImplicitExit(false);
Platform.runLater(new Runnable() {
@Override
public void run() {
try {
new Gui().start(new Stage());
} catch (Exception e) {
e.printStackTrace();
}
}
});
Scene scene = new Scene(new StackPane());
LoginManager loginManager = new LoginManager(scene);
loginManager.showLoginScreen();
stage.setScene(scene);
stage.show();
// stage.setOnCloseRequest(e -> Platform.exit());
}
}
Main class
public static void main(String[] args) throws IOException, Exception, FileNotFoundException {
ServerSocket ss = null;
try {
ss = new ServerSocket(9090);
if (ss != null) {
ss.close();
}
} catch (BindException e) {
System.out.println("Sikke Node Server is already running.");
System.exit(0);
}
launchh();
}
Method in main Class
private static void createAndShowGUI() {
if (SystemTray.isSupported()) {
final PopupMenu popup = new PopupMenu();
final TrayIcon trayIcon = new TrayIcon(createImage("/sikke24.gif", "Sikke Node "), "Sikke Node Server",
popup);
trayIcon.setImageAutoSize(true);
final SystemTray tray = SystemTray.getSystemTray();
final int port = Integer.parseInt(_System.getConfig("rpcport").get(0));
// Create a popup menu components
MenuItem aboutItem = new MenuItem("About");
Menu displayMenu = new Menu("Display");
MenuItem infoItem = new MenuItem("Info");
MenuItem noneItem = new MenuItem("None");
MenuItem exitItem = new MenuItem("Exit Sikke Node Server");
// Add components to popup menu
popup.add(aboutItem);
popup.addSeparator();
popup.add(displayMenu);
displayMenu.add(infoItem);
displayMenu.add(noneItem);
popup.add(exitItem);
trayIcon.setPopupMenu(popup);
try {
tray.add(trayIcon);
} catch (AWTException e) {
System.out.println("Sikke Node Icon could not be added.");
return;
}
trayIcon.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
/*
* JOptionPane.showMessageDialog(null,
* "Server started successfully. The server works on port number:" + port);
*/
Application.launch(Gui.class, "");
}
});
aboutItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,
"Server started successfully. The server works on port number:" + port);
}
});
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
MenuItem item = (MenuItem) e.getSource();
System.out.println(item.getLabel());
if ("Error".equals(item.getLabel())) {
trayIcon.displayMessage("Sikke Node Server", "This is an error message",
TrayIcon.MessageType.ERROR);
} else if ("Warning".equals(item.getLabel())) {
trayIcon.displayMessage("Sikke Node Server", "This is a warning message",
TrayIcon.MessageType.WARNING);
} else if ("Info".equals(item.getLabel())) {
// GUI runs
trayIcon.displayMessage("Sikke Node Server", "This is an info message",
TrayIcon.MessageType.INFO);
} else if ("None".equals(item.getLabel())) {
trayIcon.displayMessage("Sikke Node Server", "This is an ordinary message",
TrayIcon.MessageType.NONE);
}
}
};
trayIcon.displayMessage("Sikke Node Server", "Sikke Node Server started successfully on port : " + port,
TrayIcon.MessageType.INFO);
infoItem.addActionListener(listener);
noneItem.addActionListener(listener);
exitItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tray.remove(trayIcon);
System.exit(0);
}
});
}
}
Watch out here
Application.launch(Gui.class, "");
TrayIcon ActionListener updated
trayIcon.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 1) {
if (Platform.isFxApplicationThread()) {
Platform.runLater(new Runnable() {
@Override
public void run() {
try {
new Gui().start(new Stage());
} catch (Exception e) {
e.printStackTrace();
}
}
});
} else {
Application.launch(Gui.class, "");
}
}
}
});