0

I'm using dialog box from ControlFx in my JavaFX app. but on clicking Cancel button it close the application.

package testing;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import org.controlsfx.control.action.Action;
import org.controlsfx.dialog.Dialog;
import org.controlsfx.dialog.Dialogs;

public class NewFXMain extends Application {

@Override
public void start(Stage primaryStage) {
    Button btn = new Button();
    btn.setText("Say 'Hello World'");
    btn.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {
            System.out.println("Hello World!");
        }
    });

    StackPane root = new StackPane();
    root.getChildren().add(btn);

    Scene scene = new Scene(root, 300, 250);

    primaryStage.setTitle("Hello World!");
    primaryStage.setScene(scene);
    primaryStage.show();
    primaryStage.setOnCloseRequest(new EventHandler() {
        public void handle(Event t) {
            Action response = Dialogs.create()
                    .owner(new Stage())
                    .title("Exit ??")
                    .masthead("Do you want to Exit ??")
                    .actions(Dialog.Actions.OK, Dialog.Actions.CANCEL)
                    .showConfirm();

            if (response == Dialog.Actions.OK) {
                primaryStage.hide();
                System.exit(0);
// ... user chose OK
            } else if (response == Dialog.Actions.CANCEL){

            }

        }
    });

}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}

}

Am i implementing it the wrong way or is it a bug in the conrolfx ? tried warning dialog also same happens there too. I also tried other dialogs with YES, NO and cancel actions. I'm using netbeans 8.0 with jdk 8 on ubuntu 14.04.

ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176
DeepSidhu1313
  • 805
  • 15
  • 31

1 Answers1

3

OK i got this by searching through the internet and just have to consume my event and primarystage will not exit.

primaryStage.setOnCloseRequest(new EventHandler() {
    public void handle(Event t) {
        t.consume();
        Action response = Dialogs.create()
                .owner(new Stage())
                .title("Exit ??")
                .masthead("Do you want to Exit ??")
                .actions(Dialog.Actions.OK, Dialog.Actions.CANCEL)
                .showConfirm();

        if (response == Dialog.Actions.OK) {
            primaryStage.close();
            System.exit(0);

        } else if (response == Dialog.Actions.CANCEL){

        }

    }
});

Looks like very minor and silly mistake. :P

DeepSidhu1313
  • 805
  • 15
  • 31
  • How does that not put you in an infinite loop of close requests? Wouldn't it be better to say if(response != Dialog.Actions.OK) t.consume() else System.exit(0); (Of course my knowledge with JavaFX is quite limited even still and I could very likely be wrong). – Will Nov 07 '14 at 15:47