Creating a JavaFX Dialog Box

2019-09-01 06:17发布

I am coding a javafx program and i need to create and use my own Stage based (Javafx.Stage) dialog box for showing messages and confirmations. I have written all the necessary code by now but i have a problem:

A dialog box must stop execution of rest of the code until a respond is given like "yes" or "no" or "retry". When i use my dialog box like "DialogBox.ShowMessage", a stage appears with message and buttons. But, as you may think, the rest of the code continues to execute. How can i get around this? When i create the stage, it must stop the other threads or the thread that it depends on. I have searched through internet and here, but i can not find exact solution. One idea is using "javafx.event.EventDispatcher" or "javafx.event.EventDispatchChain" but i couldn't figure out how to use them. And another idea is using "java.awt.EventQueue". And here is somthing that can help: I have a control of stage show and hide events and showing or hiding eventhandlers. I think som sort of thread queue can be used in one of these spesific sections.

I hope i clarified the situation enough. Briefly, ı need to manage threads while using another stage with my own code.

Thank you.

1条回答
贼婆χ
2楼-- · 2019-09-01 06:33

About execution suspending there is a Jira issue for it http://javafx-jira.kenai.com/browse/RT-19783.
As a workaround, I have no idea how to use EventDispatcher and EventDispatchChain to overcome this problem but you can send the EventHandlers as parameter. For instance,

public static void ShowMessage(final EventHandler<ActionEvent> okAction, final EventHandler<ActionEvent> cancelAction){

   // Define and add buttons to the "view" and show message

   btnOk.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent event) {
        okAction.handle(null);
        }
    });

   btnCancel.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent event) {
        cancelAction.handle(null);
        }
    });
}

and use it as,

DialogBox.ShowMessage(
 new EventHandler<ActionEvent>() {
      @Override public void handle(ActionEvent arg0) {
        // Do stuff when "ok" clicked. 
},
 new EventHandler<ActionEvent>() {
      @Override public void handle(ActionEvent arg0) {
        // Do stuff when "cancel" clicked. 
});

I agree with this is a kind of "winding" way however.
Siteye hoş geldin ve kolay gelsin.

查看更多
登录 后发表回答