You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaFX中如何访问动态创建的HBox内无ID的TextField组件?

How to Access Dynamically Created TextFields in JavaFX Without IDs

Since you’re creating HBoxes with TextFields dynamically and haven’t set IDs, here are a few clean, maintainable ways to access those input values:

Instead of storing raw HBoxes in your rows list, create a simple class to encapsulate each row’s components. This makes accessing the TextFields explicit and avoids relying on fragile UI structure details.

First, define a class to hold your row data and UI elements:

class EdgeRow {
    private final Text indexText;
    private final TextField sepField;
    private final TextField areaField;
    private final HBox container;

    public EdgeRow(int index) {
        this.indexText = new Text(Integer.toString(index));
        this.sepField = new TextField();
        makeNumeric(this.sepField);
        this.areaField = new TextField();
        makeNumeric(this.areaField);
        
        this.container = new HBox(50);
        container.getChildren().addAll(this.indexText, this.sepField, this.areaField);
    }

    // Getters for easy access to fields
    public TextField getSepField() { return sepField; }
    public TextField getAreaField() { return areaField; }
    public HBox getContainer() { return container; }
}

Update your addRowToList() method to use this class:

// Change your rows list to hold EdgeRow instead of HBox
List<EdgeRow> rows = new ArrayList<>();

void addRowToList() { 
    numEdges++; // Assuming numEdges tracks your row count
    EdgeRow newRow = new EdgeRow(numEdges);
    rows.add(newRow);
    // Don't forget to add newRow.getContainer() to your parent UI container!
}

When you need to retrieve values later:

for (EdgeRow row : rows) {
    String sepValue = row.getSepField().getText();
    String areaValue = row.getAreaField().getText();
    // Process the values as needed (parse to numbers, validate, etc.)
}

This approach is the most robust because it decouples your data access from UI structure changes. If you ever reorder components or add new elements to the HBox, your value-retrieval code won’t break.

2. Access via HBox Child Indices

If you prefer to keep using your existing rows list of HBoxes, you can access the TextFields by their position in the HBox’s children list. Since each HBox is built with Text at index 0, sep at index 1, and area at index 2, you can cast those children directly:

for (HBox hb : rows) {
    // Get the sep TextField (position 1 in the HBox)
    TextField sepField = (TextField) hb.getChildren().get(1);
    String sepValue = sepField.getText();
    
    // Get the area TextField (position 2 in the HBox)
    TextField areaField = (TextField) hb.getChildren().get(2);
    String areaValue = areaField.getText();
    
    // Process values here
}

⚠️ Note: This method is fragile. If you ever reorder the HBox’s children or add/remove components, this code will throw errors or return incorrect elements. Use this only if you’re certain the HBox structure won’t change.

3. Assign Dynamic IDs

You can set unique IDs for each TextField when creating them, then look them up later. This works well if you need to target specific rows individually:

Modify your addRowToList() method to add unique IDs:

void addRowToList() { 
    numEdges++;
    HBox hb = new HBox(50); 
    Text num = new Text(Integer.toString(numEdges)); 
    hb.getChildren().add(num); 
    
    TextField sep = new TextField(); 
    makeNumeric(sep); 
    sep.setId("sep-row-" + numEdges); // Unique ID for sep field
    hb.getChildren().add(sep); 
    
    TextField area = new TextField(); 
    makeNumeric(area); 
    area.setId("area-row-" + numEdges); // Unique ID for area field
    hb.getChildren().add(area); 
    
    rows.add(hb); 
}

To access a specific TextField later (ensure the scene is fully rendered first):

// Get sep TextField for row 3
TextField sepField = (TextField) yourParentContainer.lookup("#sep-row-3");
// Get area TextField for row 3
TextField areaField = (TextField) yourParentContainer.lookup("#area-row-3");

Or iterate through all rows using the ID pattern:

for (int i = 1; i <= numEdges; i++) {
    TextField sep = (TextField) yourParentContainer.lookup("#sep-row-" + i);
    TextField area = (TextField) yourParentContainer.lookup("#area-row-" + i);
    // Process values
}

This method is useful for targeting specific rows, but relying on lookups can be less reliable than the custom class approach, especially if the UI is still initializing.


内容的提问来源于stack exchange,提问作者A. Gimeno

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:54:06