Question


I am working on Netbeans building a JavaFX application.
I started using ControlsFX (http://fxexperience.com/controlsfx/)
I have implemented a simple Dialog that uses custom AbstractDialogAction s as I want specific number of buttons to appear.
I do this like this:

    Action a = new AbstractDialogAction("  button a  ", Dialog.ActionTrait.CLOSING) {
        @Override
        public void execute(ActionEvent ae) {
        }
    };
    ArrayList<Action> actions = new ArrayList<>();
    actions.add(a);
    actions.add(b); // other button
    actions.add(c); // another button
    dialog.actions(actions);
    Action response =  dialog.showConfirm();

Dialog is shown correctly with the given buttons.
My question is how to force the Dialog to close when a button is pressed ?
I thought setting a Dialog.ActionTrait.CLOSING would do the trick, but the Dialog stays open.

Was it helpful?

Solution

From eugener in ControlsFX mailing list

public void execute(ActionEvent ae) {
        if (ae.getSource() instanceof Dialog ) {
          ((Dialog) ae.getSource()).setResult(this);
       }
}

The above sets the result of the Dialog to be the current Action and closes the Dialog
But maybe that is a little redundant as I can simply call:

          ((Dialog) ae.getSource()).hide();

.hide() hides the Dialog and also sets the current action as the result.
I can't suggest which is a better solution (hide() was suggested by jewelsea)

In addition I would suggest to always override the toString() method of class AbstractDialogAction, in order to get readable result from:

Action response =  dialog.showConfirm();
System.out.println("RESPONSE = "+ response.toString());

OTHER TIPS

Hide the dialog to close it => dialog.hide()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top