【问题标题】:How to use FXML controller to retrieve data from database and populate a TableView (defined in FXML)?如何使用 FXML 控制器从数据库中检索数据并填充 TableView(在 FXML 中定义)?
【发布时间】:2014-09-08 11:12:38
【问题描述】:

我是 JavaFX 和 FXML 的新手,如果这是一个非常简单的问题,请原谅我。 我想从数据库(例如 MySQL)中检索数据并将其放入 FXML 文件中定义的 TableView 中。我还想让这个 TableView 可编辑,并在用户进行一些更改时更新数据库。

我读过Mastering FXML

我找到了this post,但不是很清楚,因为没有代码示例。

我知道可以使用 DataFX。我不想那样做。

我了解可以使用控制器检索数据填充表格。我不知道怎么做最后一步。有人有代码示例吗?

假设我有一个数据库表people,其中包含first_namelast_name

我的 FXML 代码如下所示:

<GridPane fx:controller="sample.Controller" xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">

    <padding>
        <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
    </padding>

    <Label style="-fx-font: NORMAL 20 Tahoma;" text="Address Book" GridPane.columnIndex="0" GridPane.rowIndex="0">
    </Label>

    <TableView fx:id="tableView" GridPane.columnIndex="0" GridPane.rowIndex="1">
        <columns>
            <TableColumn text="First Name"></TableColumn>
            <TableColumn text="Last Name"></TableColumn>
        </columns>
    </TableView>

</GridPane>

【问题讨论】:

  • 请参阅How to populate a TableView that is defined in an fxml file 了解您问题的“填充表格”部分。 “检索数据”部分是 Java 的核心。搜索有关它的教程。但要注意使用检索到的数据更新 JavaFX GUI。即 DB 获取必须在 JavaFX 主线程以外的不同线程中。为此,请使用 JavaFX 服务和任务。再次搜索“JavaFX:后台定期 sql 查询”、“JavaFX:DB 查询时 GUI 冻结”等 GL。
  • 谢谢。对不起,我没有看到那个线程。下次会研究得更好:)
  • 没问题。搜索时请参考“高级搜索提示”。你可以相信我,这是救命稻草。
  • 为什么不想使用 DataFX?

标签: java database javafx-8 fxml


【解决方案1】:

您可以像这样为 people 表定义模型:

public class People {

  public People(String firstName, String lastName){
      this.firstName = new SimpleStringProperty(firstName);
      this.lastName = new SimpleStringProperty(lastName);
  }

  private StringProperty firstName;
  public void setFirstName(String value) { firstNameProperty().set(value); }
  public String getFirstName() { return firstNameProperty().get(); }
  public StringProperty firstNameProperty() {
    return firstName;
  }
  private StringProperty lastName;
  public void setLastName(String value) { lastNameProperty().set(value); }
  public String getLastName() { return lastNameProperty().get(); }
  public StringProperty lastNameProperty() {
    return lastName;
  }
}

然后定义一个检索数据的方法:

private ObservableList<People> fetchPeople(Connection con) throws SQLException {
    logger.info("Fetching people from database");
    ObservableList<People> people = FXCollections.observableArrayList();

    Statement st = con.createStatement();      
    ResultSet rs = st.executeQuery("select * from people");
    while (rs.next()) {
      people.add(new People(rs.getString("first_name"), rs.getString("last_name")));
    }

    return people;
  }

此方法返回一个ObservableList&lt;People&gt;,您可以使用它来填充 tableView,例如:

Connection conn = getConnection();
ObservableList<People> people = fetchPeople(conn);
ObservableList<TableColumn> columns = tableView.getColumns();
columns.get(0).setCellValueFactory(new PropertyValueFactory("firstName")); // asumming this column is the corresponding to firstName, you can also set an id to the column an refer to it with @FXML property
columns.get(1).setCellValueFactory(new PropertyValueFactory("lastName")); // asumming this column is the corresponding to lastName, you can also set an id to the column an refer to it with @FXML property
tableView.setItems(people);

使用此方法:setCellValueFactory(new PropertyValueFactory("firstName"));,我们将模型中的属性firstNameProperty 绑定到TableView 中的相应列。

此外,您可以使用函数在单元格中创建所需的值。如果您希望第三列显示全名,您可以通过以下方式获得它:

columns.get(2).setCellValueFactory(param -> {
    People p = (People)((CellDataFeatures) param).getValue();
    return new SimpleStringProperty(p.getFirstName() + " " + p.getLastName());
}); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-27
    • 1970-01-01
    • 2016-02-16
    • 2014-12-20
    • 2012-05-20
    • 1970-01-01
    • 2013-06-28
    • 1970-01-01
    相关资源
    最近更新 更多