JAVA FX - 隐藏阶段丢失服务任务响应 [英] JAVA FX - Hide stage lost service task response
问题描述
在这个帖子中回答了为什么我的UI在service.start()调用后阻塞,并且它已被解决。现在我有另一个小问题......在start()之后我会隐藏/关闭舞台以在TrayIcon上运行应用程序。
In this post i was answer why my UI was blocking after service.start() call, and it is resolved. Now I have another "little" problem... after start() the service I will hide/close the stage to run the application on TrayIcon.
我注意到了使用hide()/ close()阶段,服务将被阻止,永远不会处于成功状态。我已经尝试删除此调用并且一切正常。
I've noticed that with hide()/close() the stage, the Service will be blocked and never go in SUCCEEDED status. I've tried to remove this call and all works.
我如何隐藏()/关闭()我的阶段而不阻止服务线程因此接收答案最小化托盘?
How I can hide()/close() my stage without block the Service thread and so receive the answer for minimize on tray?
@FXML
private void handleRegisterButton() {
User newUser = new User();
newUser.setUsername(usernameTextField.getText());
newUser.setGroup(groupChoicheBox.getSelectionModel().getSelectedItem());
newUser.setMachineName(machineNameTextField.getText());
this.mainApp.registerNewUser(newUser); // this is primaryStage from Main
RegisterUserInvocation service = new RegisterUserInvocation(newUser);
service.stateProperty().addListener(new InvalidationListener() {
@Override
public void invalidated(Observable observable) {
System.out.println("Task value " + service.getState());
if(service.getState().equals(Worker.State.SUCCEEDED)) {
mainApp.showMessageOnTray("Lan Alert", "Attivo!", MessageType.INFO); // Handler method to show TrayMessage
}
}
});
service.start();
/* IF I REMOVE THIS THE APPLICATION WORKS. SURE, THE WINDOW REMAIN VISIBLE*/
this.mainApp.getPrimaryStage().close();
}
推荐答案
你需要调用 Platform.setImplicitExit(false) ,否则JavaFX运行时将关闭最后一个关闭阶段。
You need to invoke Platform.setImplicitExit(false), otherwise the JavaFX runtime will shut down when you close the last stage.
请参阅此示例,了解如何使用 TrayIcon 和JavaFX。
See this sample for using a TrayIcon and JavaFX.
这篇关于JAVA FX - 隐藏阶段丢失服务任务响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!