【问题标题】:Get all text fields values and id in javafx获取javafx中的所有文本字段值和id
【发布时间】:2025-11-20 14:45:02
【问题描述】:

我有一个带有许多文本字段和其他控件的锚窗格。我想获取所有控件的值及其名称和 ID。

例如:如何清除所有文本字段值?

【问题讨论】:

    标签: java javafx javafx-2 fxml


    【解决方案1】:

    简单的解决方案就是遍历 AnchorPane 的所有子项并寻找 TextFields:

    for (Node node : anchorPane.getChildren()) {
        System.out.println("Id: " + node.getId());
        if (node instanceof TextField) {
            // clear
            ((TextField)node).setText("");
        }
    }
    

    【讨论】:

      【解决方案2】:

      如果你需要递归的方式来测试这个,

      public class NodeUtils {
      
          public static <T extends Pane> Map<Node, Object> formValues(T parent) {
              return formValues(parent, new HashMap<>());
          }
      
          private static <T extends Pane> Map<Node, Object> formValues(T parent, Map<Node, Object> map) {
              for (Node node : parent.getChildren()) {
                  // Nodes - You can add more.
                  if (node instanceof TextField) {
                      map.put(node, ((TextField) node).getText());
                  }
                  if (node instanceof PasswordField) {
                      map.put(node, ((PasswordField) node).getText());
                  }
                  if (node instanceof TextArea) {
                      map.put(node, ((TextArea) node).getText());
                  }
                  if (node instanceof CheckBox) {
                      map.put(node, ((CheckBox) node).isSelected());
                  }
      
                  // Recursive.
                  if (node instanceof Pane) {
                      formValues((Pane) node, map);
                  }
      
              }
      
              return map;
          }
      }
      

      测试源,

      Map<Node, Object> formValues = NodeUtils.formValues(source);
      

      只获取节点

      public class NodeUtils {
      
          public static ArrayList<Node> getAllNodes(Parent root) {
              ArrayList<Node> nodes = new ArrayList<>();
              addAllDescendents(root, nodes);
              return nodes;
          }
      
          private static void addAllDescendents(Parent parent, ArrayList<Node> nodes) {
              // Get children.
              List<Node> children = Collections.EMPTY_LIST;
              if (parent instanceof ButtonBar) {
                  children = ((ButtonBar) parent).getButtons();
              } else if (parent instanceof TabPane) {
                  for (Tab tab : ((TabPane) parent).getTabs()) {
                      Node tabContent = tab.getContent();
                      if (tabContent instanceof Parent) {
                          addAllDescendents((Parent) tab.getContent(), nodes);
                      } else {
                          // You can log and get a type that is not supported.
                      }
                  }
              } else {
                  children = parent.getChildrenUnmodifiable();
              }
      
              // Add nodes.
              for (Node node : children) {
                  nodes.add(node);
                  if (node instanceof Parent) {
                      addAllDescendents((Parent) node, nodes);
                  }
              }
          }
      }
      

      测试源,

      List<Node> nodes = NodeUtils.getAllNodes(aPaneOrAnotherParentObject);
      

      【讨论】:

      • 这很好,我喜欢这个解决方案,它值得更多的紫外线;)
      【解决方案3】:

      这是一个 Java 8 版本:

      anchorPane.getChildren()
                .filtered(node -> node instanceof TextField)
                .forEach(node -> ((TextField)node).setText(""));
      

      【讨论】:

        最近更新 更多