Search code examples
javafxreturn-valuemessagebox

javafx how to get return value from message box


I have a problem I can not get a return value because i want use it in controller. How can I get a return value from checkbox after close the window. Because I need a return value in controller. Thanks

import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;

public class CheckBox {

  public static String display(String title, String message){
      Stage window = new Stage();
      String id = " ";

      window.initModality(Modality.APPLICATION_MODAL);
      window.setTitle(title);
      window.setMinWidth(250);

      Label label = new Label();
      label.setText(message);

      Button yButton = new Button("Y");
      Button nbButton = new Button("N");
      yButton.setId("Y");
      nbButton.setId("N");
      yButton.setOnAction(e -> window.close());
      nbButton.setOnAction(e -> window.close());

      VBox layout = new VBox(10);
      layout.getChildren().addAll(label,yButton, nbButton);
      layout.setAlignment(Pos.CENTER);
      Scene scene = new Scene(layout);
      window.setScene(scene);
      window.showAndWait();

      if(yButton.isPressed())
          return yButton.getId();

      else if(nbButton.isPressed())
          return nbButton.getId();

      return null;
  }
}

Solution

  • First, I would advise you not to use names that match those from standard libraries. The name CheckBox is inappropriate because it has control with that name. Use something descriptive, like CheckBoxDialog.

    Refrain from using a static context. In this case, this is unnecessary and shows a bad style.

    This is a sample implementation by keeping the static method you use.

    public class CheckBoxDialog {
        public static final String YES = "Y";
        public static final String NO = "N";
    
        private String exitCode = NO;
    
        private Stage window;
        private Label label;
    
        public CheckBoxDialog() {
            createGUI();
        }
    
        private void createGUI() {
            window = new Stage();
            window.initModality(Modality.APPLICATION_MODAL);
            window.setMinWidth(250);
    
            label = new Label();
    
            Button yButton = new Button(YES);
            Button nbButton = new Button(NO);
    
            yButton.setOnAction(e -> {
                exitCode = YES;
                window.close();
            });
    
            nbButton.setOnAction(e -> {
                exitCode = NO;
                window.close();
            });
    
            VBox layout = new VBox(10);
            layout.getChildren().addAll(label,yButton, nbButton);
            layout.setAlignment(Pos.CENTER);
    
            Scene scene = new Scene(layout);
            window.setScene(scene);
        }
    
        public void setTitle(String title) {
            window.setTitle(title);
        }
    
        public void setMessage(String message) {
            label.setText(message);
        }
    
        public String showAndWait() {
            window.showAndWait();
            return exitCode;
        }
    
        public static String display(String title, String message){
            CheckBoxDialog dlg = new CheckBoxDialog();
            dlg.setTitle(title);
            dlg.setMessage(message);
    
            return dlg.showAndWait();
        }
    }