我试图从数据(ObservableList)中填充tableview2(fx:id tableview)行。但当它完成时,tableview2只显示空记录。在控制台中,我看到数据中的所有行(ObservableList)。我知道stackoverflow中有很多关于这方面的问题,很抱歉(还有我的英语),但请你问我代码中的错误在哪里?感谢您的帮助或关注)。
更新(添加MainPayer.java和更新PayerController.java):这是我的代码。
付款人控制器。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();
}
}
}
主要付款人。Java语言
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;
}
}
主场景。fxml
<TableView fx:id="tableview2" layoutX="210.0" layoutY="72.0" prefHeight="345.0" prefWidth="981.0">
<placeholder>
<Label text="Go ahead" />
</placeholder>
<columns>
<TableColumn fx:id="tablePayerid2" minWidth="20.0" prefWidth="104.0" text="A" />
<TableColumn fx:id="tableIsAccepted2" minWidth="40.0" prefWidth="74.0" text="B" />
<TableColumn fx:id="tableYear2" minWidth="1.0" prefWidth="75.0" text="C" />
<TableColumn fx:id="tableQuater2" minWidth="1.0" prefWidth="75.0" text="D" />
<TableColumn fx:id="tableEnspeopley2" minWidth="15.0" prefWidth="75.0" text="E" />
<TableColumn fx:id="tableShortName2" minWidth="15.0" prefWidth="75.0" text="F" />
<TableColumn fx:id="tableDistrict2" prefWidth="75.0" text="J" />
<TableColumn fx:id="tableSettlement2" prefWidth="75.0" text="K" />
<TableColumn fx:id="tableStreet2" prefWidth="111.0" text="L" />
<TableColumn fx:id="tableHouseNum2" minWidth="1.0" prefWidth="67.0" text="M" />
<TableColumn fx:id="tableMailIndex2" minWidth="2.0" prefWidth="58.0" text="N" />
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TableView>
你面临着一些问题。首先,您应该了解JavaFx中TableView的基本概念。与其链接教程,不如指出您遗漏了什么,以及如何解决这些问题。
简单地解释一下TableView
有两个“部分”,一个是您可以看到的UI部分,其中包含列、标头、行等。另一边是填充表的模型。一旦您创建了UI,您必须为表分配一个模型,以将预期的数据添加到表中。
在表格视图中,行不是项目列表,而是模型的实例,每个列都有字段。(当然,它可以是一个列表,但对你来说,它让事情变得更加复杂)
有几个步骤,如果你尊重你可以得到预期的结果:
>
将此模型作为泛型传递给表视图,就像您所做的那样:private TableView tableView2-
因此,表中的每一行都是用户的一个实例,您可以像您所尝试的那样,在ObservableList中定义整个表数据,ObservableList
假设您有一个带有字符串的列:
@FXML
private TableColumn<User,String> colWithString;
然后你可以这样说:
colWithString.setCellValueFactory(data -> data.getValue().myStringProperty());
当然,您可以在需要的地方放置适当的属性,而不是myStringProperty。
就是这样,如果您遵守这些步骤并相应地修改,那么应该显示来自DB的数据。