【发布时间】:2020-11-18 01:45:40
【问题描述】:
我试图创建一个 tableview 来显示指定结果集返回的所有行,我正在使用 observableList 来存储这些行,这些行存储所有行就好了,但我只得到添加的第一行的输出。 我想说明我目前没有收到任何错误,我的代码如下。
public class StockController implements Initializable {
@FXML
private TableView tableview;
ObservableList<ObservableList> data = FXCollections.observableArrayList();
@Override
public void initialize(URL url, ResourceBundle rb) {
dbconn conn = new dbconn();
try{
String SQL = "SELECT * FROM recibido WHERE id < 3";
ResultSet rs = conn.con.createStatement().executeQuery(SQL);
for(int i = 0; i < rs.getMetaData().getColumnCount(); i++){
final int j = i;
TableColumn col = new TableColumn(rs.getMetaData().getColumnName(i+1));
col.setCellValueFactory(new Callback<CellDataFeatures<ObservableList, String>, ObservableValue<String>>(){
public ObservableValue<String> call(CellDataFeatures<ObservableList, String> param) {
return new SimpleStringProperty(param.getValue().get(j).toString());
}
});
tableview.getColumns().addAll(col);
System.out.println("Column ["+i+"] ");
}
ObservableList<String> row = FXCollections.observableArrayList();
while(rs.next()){
for(int i = 1; i<=rs.getMetaData().getColumnCount(); i++){
row.add(rs.getString(i));
}
System.out.println("Row [1] added "+row);
data.add(row);
}
tableview.setItems(data);
} catch( Exception e){
e.printStackTrace();
System.out.println("Error on Building Data");
}
}
@FXML
private void goToReception(ActionEvent event) throws IOException {
Parent reception = FXMLLoader.load(getClass().getResource("reception.fxml"));
Scene scene = new Scene(reception);
Stage receptionWindow = (Stage)((Node)event.getSource()).getScene().getWindow();
receptionWindow.setScene(scene);
receptionWindow.show();
}
}
我相信我的问题可能与不正确的启动有关
ObservableList<ObservableList> data = FXCollections.observableArrayList();
希望有人能帮助我,我已经疯了好几天了。
代码编译/输出。 enter image description here
【问题讨论】:
-
ResultSet的每个元素都需要一个新的row