my TableView
Is showing blank lines, what am I missing?
project link -> https://github.com/raphaelbgr/SwingSocketClient/tree/database
I am trying to gather information from the database and show it on this list view, the jdbc connection is perfect and working, however the tableView is not displaying the items, I've read 3 times the oracle tutorial but no luck.
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;
}
And this calls the method above:
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();
}
}
This is the DataModel used, no Idea on what I am missing on it:
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);
}
}
And a blank TableView
appears, no idea on what I am missing
Your current tableView definition is as follows:
<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>
Here, the tableColumns are missing the cellValueFactory. In FXML you can define it as
<TableColumn prefWidth="75.0" text="Timestamp">
<cellValueFactory>
<PropertyValueFactory property="timestamp" />
</cellValueFactory>
</TableColumn>
and the data model class should have the accessors:
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;
}
}
Note that for getter method that returns ObservableValue
(timestampProperty() in the above) should have the propertyName + "Property", where the propertyName is "timestamp" in <PropertyValueFactory property="timestamp" />
.
I just discovered, on the FXML file I had to input this to associate what data to incorporate on the 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>
`
And had to make a little modification on the dataModel, to retrieve the string values.
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);
}
}
And here we go, the results are here: