我正在尝试从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;
}
}
答案 0 :(得分:4)
您面临一些问题。
首先,您应该了解基本概念TableView
在JavaFx中的工作方式。
不用链接教程,也许我可以指出您错过了什么,以及如何解决这些问题。
简单解释了TableView
有两个“部分”,一个是您可以看到的 UI 部分,其中包含列,标题,行等。另一面是< strong>模型,它会填充表格。一旦创建了UI端,就必须为表分配一个模型,以将期望的数据添加到表中。
在TableView
中,行不是项目列表,而是模型的实例,该模型的每一列都有字段。 (当然,它可以是列表,但对您来说,这会使事情变得更加复杂)
如果您尊重自己的意愿,可以采取一些步骤:
User
类,其中包含要从db加载的字段,例如palayerId,isAccepted,year等。
每个字段都应该是与其类型对应的Property
,如果是String,则应该是StringProperty,如果它的长则是LongProperty,等等。将此模型作为通用模型传递给TableView
,就像您一样:private TableView tableView2
-> private TableView<User> tableView2
因此,表中的每一行都是用户的一个实例,您可以像尝试过的ObservableList<User> data
一样在ObservableList中定义整个表数据,然后像table.setItems(data)
那样进行定义。 / p>
@FXML
字段,并为每个列创建 tell ,其数据必须像这样显示:假设您有一列包含字符串:
@FXML
private TableColumn<User,String> colWithString;
然后您可以告诉它,例如:
colWithString.setCellValueFactory(data -> data.getValue().myStringProperty());
您当然不用myStringProperty
放置了适当的属性。
User
的新实例添加到先前创建的data
中,例如:data.add(new User(rs.getString(1),rs.getString(2),rs.getLong(3),...))
就是这样,如果您遵守这些步骤并进行相应的修改,则应该显示数据库中的数据。