如何在Java FX中的gridpane上动态添加行?

时间:2015-03-11 01:29:20

标签: java dynamic javafx row gridpane

我在一行中有一个DB登录表格,如下所示,

    // Hostname Input
    Label hostName = new Label("Host:");
    grid.add(hostName, 0, 2);

    final TextField host = new TextField();
    host.getText();
    GridPane.setConstraints(host, 1, 2);
    grid.getChildren().add(host);

    // Port Input
    Label portName = new Label("Port:");
    grid.add(portName, 2, 2);

    final TextField port = new TextField();
    port.getText();
    GridPane.setConstraints(port, 3, 2);
    grid.getChildren().add(port);

    // SID Input
    Label sidName = new Label("SID:");
    grid.add(sidName, 4, 2);

    final TextField sid = new TextField();
    sid.getText();
    GridPane.setConstraints(sid, 5, 2);
    grid.getChildren().add(sid);

    // Username Input
    Label userName = new Label("Username:");
    grid.add(userName, 6, 2);

    final TextField user = new TextField();
    user.getText();
    GridPane.setConstraints(user, 7, 2);
    grid.getChildren().add(user);

    // Password Input
    Label pw = new Label("Password:");
    grid.add(pw, 8, 2);

    final TextField password = new PasswordField();
    password.getText();
    GridPane.setConstraints(password, 9, 2);
    grid.getChildren().add(password);

我需要在以下情况下添加更多行:

  1. 当txt输入文件有多行时。

    ex)当有一行时,不需要延伸,   当两个时,需要再添加一行。

  2. 当用户点击按钮添加行时。

  3. 是否可以在Java FX中执行此操作? 请帮忙!!

    谢谢你的期待!

1 个答案:

答案 0 :(得分:1)

为什么在阅读文本文件时不要使用gridPane.addRow()

try(BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
    StringBuilder sb = new StringBuilder();
    String line = br.readLine();
    int lineCounter = 0;

    while (line != null) {
        lineCounter++;
        if(lineCounter > 1){
            gridPane.addRow(lineCounter-1, nodeThatYouWant);
        }
        sb.append(line);
        sb.append(System.lineSeparator());
        line = br.readLine();
    }
    String everything = sb.toString();
}