JavaFX:如何在自定义对话框中重写按钮操作

huangapple go评论87阅读模式
英文:

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 (&#39;ButtonType.FINISH&#39; or &#39;ButtonType.CANCEL&#39;). My goal is to override default action on finish and close button and I want to return list instead of return button type.

It&#39;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&lt;List&lt;String&gt;&gt; {
    ArrayList&lt;String&gt; returnList = new ArrayList&lt;&gt;();
    public MyDialog() {
        returnList.add(&quot;test 1&quot;);
        returnList.add(&quot;test 2&quot;);
        this.getDialogPane().getButtonTypes().addAll(ButtonType.FINISH, ButtonType.CANCEL);

        setResultConverter(dialogButton -&gt; {
           if (dialogButton == ButtonType.FINISH) {
               return returnList;
           }
           return new ArrayList&lt;&gt;();
        });
    }
}

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);
    }
}

huangapple
  • 本文由 发表于 2020年7月26日 03:53:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/63092923.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定