获取javafx中的所有文本字段值和id

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19804751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 20:19:55  来源:igfitidea点击:

Get all text fields values and id in javafx

javajavafxjavafx-2fxml

提问by Anshul Parashar

I have an anchor pane with many text fields and other controls. I want to take values of all controls and their names and id.

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

For example: how can I clear all textfield values?

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

采纳答案by Sergey Grinev

Straightforward solution would be just traversing all children of AnchorPane and looking for TextFields:

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

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

回答by George Siggouroglou

In case you need a recursive way test this,

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

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

Test source,

测试源,

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

Get only the nodes

只获取节点

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

Test source,

测试源,

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

回答by ddnStack

Heres a Java 8 version:

这是 Java 8 版本:

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