DeepSidhu1313
DeepSidhu1313

Reputation: 805

JAVAFX-8 CANCEL ACTION Close the stage, using Dialog from controlfx in Javafx?

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.

Upvotes: 0

Views: 4051

Answers (1)

DeepSidhu1313
DeepSidhu1313

Reputation: 805

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

Upvotes: 3

Related Questions