如何从运行时添加的javafx TextField中获取文本?

时间:2015-08-15 16:19:39

标签: java javafx fxml

我在场景中有两个按钮,一个触发方法newFields(),在TextField内部生成两个新HBox,另一个通过getText()方法生成 <VBox prefHeight="275.0" prefWidth="300.0" fx:id="pane"> <children> <Button layoutX="6.0" layoutY="14.0" mnemonicParsing="false" onAction="#newFields" text="New Fields" /> <Button layoutX="6.0" layoutY="53.0" mnemonicParsing="false" onAction="#getTexts" text="Get Texts" /> </children> </VBox> 喜欢它来获取打字文本。

sample.fxml:

public class Controller {

    @FXML VBox pane;    
    @FXML void newFields(){    
        HBox hb = new HBox();
        pane.getChildren().add(hb);

        TextField tf = new TextField();
        tf.setPromptText("Name here");    
        TextField tf2 = new TextField();
        tf2.setPromptText("Age here");

        hb.getChildren().addAll(tf, tf2);
    }    
    @FXML void getTexts(){    
        ObservableList<Node> childsVB = pane.getChildren();
        Node hb = childsVB.get(2);
        hb.getChildren();//Cannot resolve method
    }
}

Controller.java:

HBox

我有Texfield的节点包含HBox但是我被卡在那里,我试图找到//either define parent object scope only $scope.new = {} //or if you want to define child object too . May be to show some default value . $scope.new = { CustomerID: '', //empty or may be some default value CompanyName: '' } 的孩子,但IDE会抛出错误。

我是这里的新用户,我确实在寻找答案,但没有成功。

提前致谢。

1 个答案:

答案 0 :(得分:0)

Node没有getChildren()方法。你应该使用Casting。 变化:

Node hb = childsVB.get(2);

为:

HBox hb = (HBox)childsVB.get(2);

或使用 Pane class

Pane pane = (Pane)childsVB.get(2);
相关问题