2017-11-29 25 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을 클릭하면 ID 인 Y를 반환하고 @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