gpt4 book ai didi

java - TableView (javaFX) 仅显示空行

转载 作者:行者123 更新时间:2023-11-30 08:12:44 24 4
gpt4 key购买 nike

我的TableView显示空行,我错过了什么?

项目链接-> https://github.com/raphaelbgr/SwingSocketClient/tree/database

我试图从数据库收集信息并将其显示在这个 ListView 上,jdbc 连接完美并且工作正常,但是 tableView 没有显示项目,我已经阅读了 3 遍 oracle 教程但没有运气。

public ObservableList<MessageDataTableModel> queryChatHistory(int rowLimit) throws SQLException {
final ObservableList<MessageDataTableModel> data = FXCollections.observableArrayList();
String query = "SELECT SERV_REC_TIMESTAMP, OWNERNAME, TEXT FROM MESSAGELOG LIMIT " + rowLimit;

Statement st = c.createStatement();
ResultSet rs = st.executeQuery(query);
while(rs.next()) {
while(rs.next()) {
data.add(new MessageDataTableModel(rs.getString(1),rs.getString(2),rs.getString(3)));
}
}
return data;
}

这会调用上面的方法:

public void populateHistoryTable(int rowLimit) {
DAO dao = new DAO();
try {
dao.connect();
table_chathistory.setItems(dao.queryChatHistory(rowLimit));
dao.disconnect();
} catch (SQLException e) {
e.printStackTrace();
}
}

这是使用的数据模型,不知道我缺少什么:

private SimpleStringProperty timestamp = new SimpleStringProperty();
private SimpleStringProperty screenname = new SimpleStringProperty();
private SimpleStringProperty message = new SimpleStringProperty();

public MessageDataTableModel() {

}

public MessageDataTableModel(String timestamp, String screenname, String message) {
this.timestamp = new SimpleStringProperty(timestamp);
this.screenname = new SimpleStringProperty(screenname);
this.message = new SimpleStringProperty(message);
}
public void setTimestamp(String timestamp) {
this.timestamp.set(timestamp);
}
public void setScreenname(String screenname) {
this.screenname.set(screenname);
}
public void setMessage(String message) {
this.message.set(message);
}
}

出现一个空白的TableView,不知道我错过了什么

And a blank list View appears, no idea on what I am missing

最佳答案

您当前的 tableView 定义如下:

<TableView fx:id="table_chathistory" layoutX="7.0" layoutY="31.0" prefHeight="354.0" prefWidth="602.0">
<columns>
<TableColumn prefWidth="75.0" text="Timestamp" />
<TableColumn prefWidth="85.0" text="Screen Name" />
<TableColumn prefWidth="441.0" text="Message" />
</columns>
</TableView>

这里,tableColumns 缺少 cellValueFactory 。在 FXML 中您可以将其定义为

<TableColumn prefWidth="75.0" text="Timestamp">
<cellValueFactory>
<PropertyValueFactory property="timestamp" />
</cellValueFactory>
</TableColumn>

并且数据模型类应该具有访问器:

public class MessageDataTableModel
{

private StringProperty timestampProperty = new SimpleStringProperty();

public void setTimestamp( String timestamp ) {
this.timestampProperty.set( timestamp );
}

public String getTimestamp() {
return this.timestampProperty.get();
}

public StringProperty timestampProperty() {
return this.timestampProperty;
}
}

请注意,对于返回 ObservableValue 的 getter 方法(上面的timestampProperty())应该有propertyName + “Property”,其中propertyName是<PropertyValueFactory property="timestamp" />中的“timestamp” .

关于java - TableView (javaFX) 仅显示空行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30146414/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com