的TableView(JavaFX的)是仅示出空白行(TableView (javaFX) is s

2019-10-23 02:07发布

TableView是显示空行,我缺少什么?

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

我试图从数据库中收集信息,并显示在此列表视图中,JDBC连接是完美的工作,但是的tableView不显示的项目,我读过3倍甲骨文教程,但没有运气。

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出现后,我缺少的是什么都不知道

Answer 1:

您当前的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>

在这里,TableColumn来缺少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 (timestampProperty()在上述)应具有propertyName的+“属性”,其中propertyName为在“时间戳” <PropertyValueFactory property="timestamp" />



Answer 2:

我刚刚发现,在FXML文件,我不得不输入此关联到对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"><cellValueFactory><PropertyValueFactory property="timestamp" /></cellValueFactory></TableColumn>

<TableColumn prefWidth="85.0" text="Screen Name"><cellValueFactory><PropertyValueFactory property="screenname" /></cellValueFactory></TableColumn>

<TableColumn prefWidth="441.0" text="Message"><cellValueFactory><PropertyValueFactory property="message" /></cellValueFactory></TableColumn> </columns> `

而不得不做出对数据模型稍加修改,以检索字符串值。

public class MessageDataTableModel {

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 String getTimestamp() {
    return timestamp.getValue();
}
public void setTimestamp(String timestamp) {
    this.timestamp.set(timestamp);
}
public String getScreenname() {
    return screenname.getValue();
}
public void setScreenname(String screenname) {
    this.screenname.set(screenname);
}
public String getMessage() {
    return message.getValue();
}
public void setMessage(String message) {
    this.message.set(message);
}
}

在这里,我们走,结果在这里:



文章来源: TableView (javaFX) is showing only blank lines