英文:
JavaFX: How to overwrite button action in custom Dialog
问题
我在基于JDK 8和JavaFX的桌面应用程序上工作。
我创建了一个自定义对话框类,其中包含2个按钮(完成和取消)。我的目标是在对话框中点击“完成”按钮后返回添加在对话框中的字符串列表(取消操作会返回空列表)。
我遇到了问题,因为showAndWait函数返回我点击的按钮类型('ButtonType.FINISH'或'ButtonType.CANCEL')。我的目标是覆盖完成和关闭按钮上的默认操作,我希望返回字符串列表而不是按钮类型。
虽然始终可以创建自定义按钮,但最好使用JavaFX已提供的按钮。
作为响应,您可以使用任何JVM语言(Java/Kotlin/Scala)。
代码:
```kotlin
class MyDialog : Dialog<MutableList<String>>() {
val listToReturn: MutableList<String> = mutableListOf()
init {
val dialogPane: DialogPane = this.dialogPane
dialogPane.buttonTypes.addAll(ButtonType.FINISH, ButtonType.CANCEL)
}
}
val myDialog: MyDialog = MyDialog()
// 这里我得到的是ButtonType('ButtonType.FINISH'或'ButtonType.CANCEL'),而不是字符串列表
myDialog.showAndWait().ifPresent { list -> println(list) }
<details>
<summary>英文:</summary>
I work on desktop application based on JDK 8 and JavaFX.
I created custom dialog class with 2 buttons(finish and cancel). My goal is to return the list of strings added in dialog (after clicking finish button, dialog returns list. Cancel makes return empty list).
I have problem, beacause function showAndWait return type of button which I clicked ('ButtonType.FINISH' or 'ButtonType.CANCEL'). My goal is to override default action on finish and close button and I want to return list instead of return button type.
It's always possible to create custom buttons, however, it would be better to use those already provided by JavaFX.
In response, you can use any of the JVM languages (Java/Kotlin/Scala).
Code:
class MyDialog : Dialog<MutableList<String>>() {
val listToReturn: MutableList<String> = mutableListOf()
init {
val dialogPane: DialogPane = this.dialogPane
dialogPane.buttonTypes.addAll(ButtonType.FINISH, ButtonType.CANCEL)
}
}
val myDialog: MyDialog = MyDialog()
// here I got ButtonType ('ButtonType.FINISH' or 'ButtonType.CANCEL'), not list of string
myDialog.showAndWait().ifPresent { list -> println(list) }
</details>
# 答案1
**得分**: 2
你需要使用一个结果转换器
```java
public class MyDialog extends Dialog<List<String>> {
ArrayList<String> returnList = new ArrayList<>();
public MyDialog() {
returnList.add("test 1");
returnList.add("test 2");
this.getDialogPane().getButtonTypes().addAll(ButtonType.FINISH, ButtonType.CANCEL);
setResultConverter(dialogButton -> {
if (dialogButton == ButtonType.FINISH) {
return returnList;
}
return new ArrayList<>();
});
}
}
对于应用程序端
public class Main extends Application {
public static void main(String[] args) {
launch();
}
@Override
public void start(Stage primaryStage) {
MyDialog myDialog = new MyDialog();
myDialog.showAndWait().ifPresent(System.out::println);
}
}
英文:
You need to use a result converter
public class MyDialog extends Dialog<List<String>> {
ArrayList<String> returnList = new ArrayList<>();
public MyDialog() {
returnList.add("test 1");
returnList.add("test 2");
this.getDialogPane().getButtonTypes().addAll(ButtonType.FINISH, ButtonType.CANCEL);
setResultConverter(dialogButton -> {
if (dialogButton == ButtonType.FINISH) {
return returnList;
}
return new ArrayList<>();
});
}
}
and for the application side
public class main extends Application {
public static void main (String [] args) {
launch();
}
@Override
public void start(Stage primaryStage) {
MyDialog myDialog = new MyDialog();
myDialog.showAndWait().ifPresent(System.out::println);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论