简体   繁体   中英

Getting value of a Text Field from a VBox

I am currently adding a TextField and a corresponding ComboBox dynamically every time a button is pressed. Is there a way outside of my method to then use the VBox (fieldContainer) variable to get the TextField and ComboBox values?

EDIT

I am creating an application in which the user can connect to a database and create a table. The scene in which the user creates the table has a TextField for the table name and a TextField for the column name with a corresponding ComboBox to select the column type.

创建表场景

when the user clicks on add field, it generates another TextField and ComboBox bellow the current one, so now the table as two columns, etc...

I then later want to grab the values (with the code bellow) when the user clicks create so I can organize it as a proper SQL statement.

Code

addTableField.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {             
        HBox box = new HBox(10);

        ComboBox<String> combo = new ComboBox<String>(fieldTypes);
        combo.setPromptText("type");

        TextField field = new TextField();
        field.setPromptText("field label");

        box.getChildren().addAll(field, combo);

        fieldContainer.getChildren().addAll(box);
        window.sizeToScene();
    }
});

You can do this by creating a class to hold the data which (if I understand correctly) will form each row of the resulting table. Create an object from this class when you create the HBox , bind the data in the object to the data in the controls, and add the object to a list. Then later you can just examine the contents of the list.

Something like:

public class Row {
    private final StringProperty label = new SimpleStringProperty() ;
    public StringProperty labelProperty() {
        return label ;
    }
    public final String getLabel() {
        return labelProperty().get();
    }
    public final void setLabel(String label) {
        labelProperty().set(label);
    }

    public final StringProperty type = new SimpleStringProperty();
    public StringProperty typeProperty() {
        return type ;
    }
    public final String getType() {
        return typeProperty().get();
    }
    public final void setType(String type) {
       typeProperty().set(type);
    }
}

Now in your main code you do:

final List<Row> rows = new ArrayList<>();

addTableField.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {             
        HBox box = new HBox(10);

        ComboBox<String> combo = new ComboBox<String>(fieldTypes);
        combo.setPromptText("type");

        TextField field = new TextField();
        field.setPromptText("field label");

        box.getChildren().addAll(field, combo);

        fieldContainer.getChildren().addAll(box);

        Row row = new Row();
        rows.add(row);
        row.labelProperty().bind(field.textProperty());
        row.typeProperty().bind(combo.valueProperty()); // might need to worry about null values...

        window.sizeToScene();
    }
});

Then when the user clicks "Create" you can just iterate through rows , which will have one object for each HBox you created, with getLabel() and getType() giving the values in the corresponding controls in the respective HBox .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM