[英]How to return result from event handler in javafx?
如何從javafx中的事件處理程序返回結果? 我有下面的代碼,如何從事件返回數據到函數showPrompt? 是否可以為事件的功能恢復數據?
public static String showPrompt(String title, String defValue){
final Stage dlgStage = new Stage();
TextField txtPromptValue = new TextField(defValue);
Button btnOk = new Button("Ok");
Button btnCancel = new Button("Cancel");
btnOk.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent arg0) {
//How to return data from event to function?
dlgStage.close();
}
});
btnCancel.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent arg0) {
//How to return data from event to function?
dlgStage.close();
}
});
//
Label lblTitle = new Label(title);
lblTitle.setFont(Font.font("Amble CN", FontWeight.NORMAL, 14));
//
VBox vbox = new VBox(lblTitle,txtPromptValue,btnOk,btnCancel);
vbox.setAlignment(Pos.CENTER);
vbox.setMinSize(300, 200);
//
Scene dlgScene = new Scene(vbox);
//
dlgStage.setScene(dlgScene);
dlgStage.initStyle(StageStyle.UNDECORATED);
dlgStage.initModality(Modality.APPLICATION_MODAL);
dlgStage.setMinWidth(300);
dlgStage.setMinHeight(200);
dlgStage.show();
}
簡短的答案是您無法返回值。
下面的代碼稱為callback
。
new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent arg0) {
dlgStage.close();
}
}
回調沒有返回類型,如您在上面的示例中看到的那樣,它是void
。
回調是方法 ,您作為參數傳遞給另一個方法。 另一個方法會在需要時調用回調方法 。 這意味着回調是異步的。
在您的示例中,當您按下按鈕時,它將調用回調 。
總之,您不能使用return
從中return
。
您可以從回調中調用方法,並將return
值作為參數發送給該方法。
例:
btnCancel.setOnAction(
new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent arg0) {
YourClass.setReturnValue("This is button Cancel");
dlgStage.close();
}
}
});
setReturnValue
是屬於YourClass
的方法或其實例,因此它將零售您的返回值。
更好的方法的另一種方式是創建可能擴展Stage
的類。 同樣在showPrompt
方法中,您將必須使用showAndWait()
或類似方法阻止執行。
總之,您不能僅通過一種方法來創建整個Prompt
。
您不能這樣做 ,因為在打開和關閉提示階段之前,主線程已經通過了showPrompt
方法。
正如安德烈說 ,你需要做的就是創建自己的自定義PromptStage
與showPrompt
API,阻止主線程,直到提示階段被關閉。
public static String showPrompt(final String title, final String defValue)
{
// This line will block the main thread
// See the "showAndWait()" API from JavaFX
final boolean result = PromptStage.showPrompt("My Prompt Stage", " ");
// And when the stage is closed, it will carry on to this piece of code
if (result)
{
return "This is button OK";
}
else
{
return "This is button CANCEL";
}
}
或者,如果您願意,甚至可以創建PromptDialog
實例
public static String showPrompt(final String title, final String defValue)
{
final PromptStage pStage = new PromptStage();
// This line will block the main thread
// See the "showAndWait()" API from JavaFX
pStage.showAndWait();
return pStage.getResultAsString();
}
這里有很多方法。 老實說,我不會為您寫整個課程。 但是,如果遇到問題,請發表評論。
另一個選擇是將showPrompt(...)
方法傳遞給StringProperty
,並在“確定”按鈕的處理程序中更新屬性。 然后, showPrompt
的調用者可以創建StringProperty
, showPrompt
其注冊一個偵聽器並進行觀察。 就像是:
public String showPrompt(String title, String defValue, final StringProperty result){
// ...
final TextField txtPromptValue = new TextField(defValue);
// ...
btnOk.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent event) {
result.set(txtPromptValue.getText());
dlgStage.close();
}
});
// ...
}
然后,您使用類似以下內容的對話框:
StringProperty dialogResult = new SimpleStringProperty();
dialogResult.addListener(new ChangeListener<String>() {
public void changed(ObservableValue<? extends String> obs, String oldValue, String newValue) {
// process newValue, the value from the dialog...
}
});
showPrompt("Dialog Title", "Default value", dialogResult);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.