从ObservableList填充tableView(JavaFX)无法正常工作

时间:2019-05-22 07:36:19

标签: java oracle javafx fxml

我正在尝试从data(ObservableList)中填充tableview2(fx:id tableview)行。但是,完成后,tableview2仅显示空记录。在控制台中,我看到data(ObservableList)中的所有行。我知道在stackoverflow中有多少个问题,对此感到抱歉(和我的英语),但是请问我代码中的错误在哪里?感谢您的帮助或关注。)

更新(添加MainPayer.java并更新PayerController.java): 这是我的代码。

PayerController.java:

payload

MainPayer.java

public class PayerController {
@FXML
private TableColumn<MainPayer,Long> tablePayerid2;
@FXML
private TableColumn<MainPayer,Long> tableIsAccepted2;
@FXML
private TableColumn<MainPayer,Long> tableYear2;
@FXML
private TableColumn<MainPayer,Long> tableQuater2;
@FXML
private TableColumn<MainPayer,Long> tableEnspeopley2;
@FXML
private TableColumn<MainPayer,String> tableShortName2;
@FXML
private TableColumn<MainPayer,String> tableDistrict2;
@FXML
private TableColumn<MainPayer,String> tableSettlement2;
@FXML
private TableColumn<MainPayer,String> tableStreet2;
@FXML
private TableColumn<MainPayer,String> tableHouseNum2;
@FXML
private TableColumn<MainPayer,String> tableMailIndex2;
@FXML
private TableView<MainPayer> tableview2;
private ObservableList<MainPayer> data;

@FXML
    private void handleRunMainQuery() throws SQLException {

        Connection dbConnection = null;
        Statement statement = null;

        String selectTableSQL = "SELECT        F.PAYERID, F.ISACCEPTED, F.RYEAR, F.QUATER, F.ENSPEOPLEY, S.CSHORTNAME, T.DISTRICT, T.SETTLEMENT, T.STREETNAME, T.HOUSENUM, \n" +
                "                         T.MAILINDEX\n" +
                "FROM            FFUNDSIDE F, FPAYER S, FLOCATIONHISTORY T,\n" +
                "                             (SELECT        PAYERID, MAX(LADATE) AS LADATE\n" +
                "                               FROM            FLOCATIONHISTORY\n" +
                "                               GROUP BY PAYERID) X\n" +
                "WHERE        F.PAYERID = S.PAYERID AND S.PAYERID = T.PAYERID AND T.PAYERID = X.PAYERID AND T.LADATE = X.LADATE AND (F.PAYERID BETWEEN \n" +
                "                         400000000 AND 499999999) AND (F.RYEAR = 2018) AND (F.QUATER = 3) AND (F.ENSPEOPLEY > 10)";

        data = FXCollections.observableArrayList();
        try {
            dbConnection = getDBConnection();
            statement = dbConnection.createStatement();

            ResultSet rs = statement.executeQuery(selectTableSQL);


            tablePayerid2.setCellValueFactory(data -> data.getValue().payerid2Property().asObject());
        tableIsAccepted2.setCellValueFactory(data -> data.getValue().isAccepted2Property().asObject());
        tableYear2.setCellValueFactory(data -> data.getValue().year2Property().asObject());
        tableQuater2.setCellValueFactory(data -> data.getValue().quater2Property().asObject());
        tableEnspeopley2.setCellValueFactory(data -> data.getValue().enspeopley2Property().asObject());
        tableShortName2.setCellValueFactory(data -> data.getValue().shortName2Property());
        tableDistrict2.setCellValueFactory(data -> data.getValue().district2Property());
        tableSettlement2.setCellValueFactory(data -> data.getValue().settlement2Property());
        tableStreet2.setCellValueFactory(data -> data.getValue().street2Property());
        tableHouseNum2.setCellValueFactory(data -> data.getValue().houseNum2Property());
        tableMailIndex2.setCellValueFactory(data -> data.getValue().mailIndex2Property());

            while (rs.next()) {
            ObservableList<MainPayer> row = FXCollections.observableArrayList();
            data.add(new MainPayer(rs.getLong(1),rs.getLong(2),rs.getLong(3),rs.getLong(4),rs.getLong(5),
                    rs.getString(6),rs.getString(7),rs.getString(8),rs.getString(9),rs.getString(10),
                    rs.getString(11)));
        }

            tableview2.setItems(data);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        } finally {
            if (statement != null) {
                statement.close();
            }
            if (dbConnection != null) {
                dbConnection.close();
            }
        }
}

MainScene.fxml

public class MainPayer {
private final LongProperty payerid2;
    private final LongProperty isAccepted2;
    private final LongProperty year2;
    private final LongProperty quater2;
    private final LongProperty enspeopley2;
    private final StringProperty shortName2;
    private final StringProperty district2;
    private final StringProperty settlement2;
    private final StringProperty street2;
    private final StringProperty houseNum2;
    private final StringProperty mailIndex2;

public MainPayer(Long payerid2, Long isAccepted2, Long year2, Long quater2, Long enspeopley2, String shortName2, String district2,
                 String settlement2, String street2, String houseNum2, String mailIndex2) {
    this.payerid2 = new SimpleLongProperty(payerid2);
    this.isAccepted2 = new SimpleLongProperty(isAccepted2);
    this.year2 = new SimpleLongProperty(year2);
    this.quater2 = new SimpleLongProperty(quater2);
    this.enspeopley2 = new SimpleLongProperty(enspeopley2);
    this.shortName2 = new SimpleStringProperty(shortName2);
    this.district2 = new SimpleStringProperty(district2);
    this.settlement2 = new SimpleStringProperty(settlement2);
    this.street2 = new SimpleStringProperty(street2);
    this.houseNum2 = new SimpleStringProperty(houseNum2);
    this.mailIndex2 = new SimpleStringProperty(mailIndex2);
}

public LongProperty payerid2Property() {
    return payerid2;
}
public LongProperty isAccepted2Property() {
    return isAccepted2;
}
public LongProperty year2Property() {
    return year2;
}
public LongProperty quater2Property() {
    return quater2;
}
public LongProperty enspeopley2Property() {
    return enspeopley2;
}
public StringProperty shortName2Property() {
    return shortName2;
}
public StringProperty district2Property() {
    return district2;
}
public StringProperty settlement2Property() {
    return settlement2;
}
public StringProperty street2Property() {
    return street2;
}
public StringProperty houseNum2Property() {
    return houseNum2;
}
public StringProperty mailIndex2Property() {
    return mailIndex2;
}
}

1 个答案:

答案 0 :(得分:4)

您面临一些问题。 首先,您应该了解基本概念TableView在JavaFx中的工作方式。 不用链接教程,也许我可以指出您错过了什么,以及如何解决这些问题。

简单解释了TableView有两个“部分”,一个是您可以看到的 UI 部分,其中包含列,标题,行等。另一面是< strong>模型,它会填充表格。一旦创建了UI端,就必须为表分配一个模型,以将期望的数据添加到表中。

TableView中,行不是项目列表,而是模型的实例,该模型的每一列都有字段。 (当然,它可以是列表,但对您来说,这会使事情变得更加复杂)

如果您尊重自己的意愿,可以采取一些步骤:

  1. 创建一个calass作为模型,它代表您拥有的数据。在您的情况下,我看到从数据库中加载了一个用户或某物,因此也许您可以创建一个User类,其中包含要从db加载的字段,例如palayerId,isAccepted,year等。 每个字段都应该是与其类型对应的Property,如果是String,则应该是StringProperty,如果它的长则是LongProperty,等等。
  2. 将此模型作为通用模型传递给TableView,就像您一样:private TableView tableView2-> private TableView<User> tableView2

  3. 因此,表中的每一行都是用户的一个实例,您可以像尝试过的ObservableList<User> data一样在ObservableList中定义整个表数据,然后像table.setItems(data)那样进行定义。 / p>

  4. 现在表知道有关数据的信息,但是列尚未知道,因此您必须告诉数据所属的每一列。您必须像为tableView一样为每个列创建一个@FXML字段,并为每个列创建 tell ,其数据必须像这样显示:

假设您有一列包含字符串:

@FXML
private TableColumn<User,String> colWithString;

然后您可以告诉它,例如:

colWithString.setCellValueFactory(data -> data.getValue().myStringProperty());

您当然不用myStringProperty放置了适当的属性。

  1. 在for循环中,您只需将User的新实例添加到先前创建的data中,例如:data.add(new User(rs.getString(1),rs.getString(2),rs.getLong(3),...))

就是这样,如果您遵守这些步骤并进行相应的修改,则应该显示数据库中的数据。