【问题标题】:How to programmatically get text from TextField如何以编程方式从 TextField 获取文本
【发布时间】:2018-09-29 18:00:49
【问题描述】:

我有这段代码来查找所有“id”名称,但我现在想使用这些名称从同一个 fxml 中获取文本。

@FXML
private void handleButtonAction(ActionEvent event) {


    Node source = (Node) event.getSource();
    Scene scene = source.getScene();
    ObservableList<Node> stringArray = source.getParent().getChildrenUnmodifiable();
    for (int index = 0; index < stringArray.size(); index++) {
        if (stringArray.get(index).getId() != null) {
            System.out.println(stringArray.get(index).getId());
        }
    }

}

【问题讨论】:

  • source.getParent() 转换为Node 并使用Node.lookup("") 获取TextField 再次转换为TextField 并获取您的文本.. 记得将fx:id= 添加到您的TextField @ 987654329@无需循环
  • get the text from the same fxml 对我来说不太明白,你是什么意思?

标签: javafx textfield actionevent


【解决方案1】:

fx:id 设置为您的 TextField 并从代码中调用它。 在 fxml 中看起来:

<TextField fx:id="yourTextField"/>

所以你可以使用@FXML注解在java-class中声明一个TextField变量

@FXML
TextField yourTextField;

yourTextField.getText();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-26
    • 2010-09-08
    • 1970-01-01
    • 1970-01-01
    • 2015-08-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多