Get all text fields values and id in javafx
Asked Answered
I

3

7

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.

For example: how can I clear all textfield values?

Icecap answered 6/11, 2013 at 5:26 Comment(0)
I
10

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

for (Node node : anchorPane.getChildren()) {
    System.out.println("Id: " + node.getId());
    if (node instanceof TextField) {
        // clear
        ((TextField)node).setText("");
    }
}
Irrecoverable answered 6/11, 2013 at 7:30 Comment(0)
S
7

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);
Scuttlebutt answered 2/8, 2016 at 9:49 Comment(1)
this is nice i like this solution it deserve more uv ;)Og
D
3

Heres a Java 8 version:

anchorPane.getChildren()
          .filtered(node -> node instanceof TextField)
          .forEach(node -> ((TextField)node).setText(""));
Dogberry answered 10/11, 2017 at 15:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.