2017-11-29 20 views
-1

問題があります。コントローラで使用したいので、戻り値を取得できません。ウィンドウを閉じた後、チェックボックスから戻り値を取得するにはどうすればよいですか?私はコントローラに戻り値が必要なので。javafxメッセージボックスから戻り値を取得する方法

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; 
    } 
} 
+0

正確にこのダイアログから戻っておきたいことはありますか? –

+0

ユーザがYbuttonをクリックした場合、私はYであるIDを返し、コントローラ@mrmcwolfに文字列 "Y"を返したいと思います – JhihweiLi

答えて

1

まずおかげで、私はあなたが標準ライブラリからのものと一致する名前を使用しないように助言します。 CheckBoxという名前は、その名前で制御されているため不適切です。説明的なもの(CheckBoxDialogなど)を使用してください。

静的コンテキストを使用しないようにします。この場合、これは不要でスタイルが悪いことを示します。

これは、使用する静的メソッドを保持することによって実装されたサンプルです。

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("Y"); 
     Button nbButton = new Button("N"); 

     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(); 
    } 
} 
+0

ありがとうございました。できます。 – JhihweiLi

関連する問題