TableView (javaFX) 只显示空行
TableView (javaFX) is showing only blank lines
我的 TableView
显示空白行,我错过了什么?
项目link -> https://github.com/raphaelbgr/SwingSocketClient/tree/database
我正在尝试从数据库中收集信息并将其显示在此列表视图中,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
,不知道我错过了什么
我刚发现,在 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>
`
并且必须对 dataModel 进行一些修改,以检索字符串值。
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定义如下:
<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>
和数据模型 class 应该有访问器:
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;
}
}
请注意,对于 getter 方法,returns ObservableValue
(上面的 timestampProperty())应该具有 propertyName + "Property",其中 propertyName 是 "timestamp" 在 <PropertyValueFactory property="timestamp" />
.
我的 TableView
显示空白行,我错过了什么?
项目link -> https://github.com/raphaelbgr/SwingSocketClient/tree/database
我正在尝试从数据库中收集信息并将其显示在此列表视图中,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
,不知道我错过了什么
我刚发现,在 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>
`
并且必须对 dataModel 进行一些修改,以检索字符串值。
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定义如下:
<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>
和数据模型 class 应该有访问器:
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;
}
}
请注意,对于 getter 方法,returns ObservableValue
(上面的 timestampProperty())应该具有 propertyName + "Property",其中 propertyName 是 "timestamp" 在 <PropertyValueFactory property="timestamp" />
.