i am trying to populate a tableview with data from a sqlite-db, but it is not really working. the bug must be somewhere in the "col.setCellValueFactory(...)".
i am not really into javafx, so it would be nice if you could help me out :)
thanks!
public class DynamicTable{
ObservableList<ObservableList> data;
@FXML TableView tableview;
public void buildData(String table, String filter){
tableview=new TableView();
String SQL=null;
Connection c= DBController.getConnection();
data = FXCollections.observableArrayList();
try{
switch(table){
case "Kuenstler":{
SQL="SELECT * FROM Kuenstler";
break;
}
case "Kunstwerke":{
SQL="SELECT * FROM Kunstwerk WHERE Kunststil_Name='"+filter+"'";
break;
}
case "Museen":{
SQL="SELECT Museum.Name, Adresse.Stadt FROM Museum JOIN Gebaeude JOIN Adresse ON Museum.GebaeudeID=Gebaeude.GebaeudeID AND Adresse.AdressID=Gebaeude.AdressID WHERE Stadt='"+filter+"'";
break;
}
case "Sammlungen":{
SQL="SELECT * FROM Sammlung";
break;
}
}
ResultSet rs = c.createStatement().executeQuery(SQL);
//Bug must be in this for-loop
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+"] ");
}
while(rs.next()){
ObservableList<String> row = FXCollections.observableArrayList();
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");
}
}
}
Aucun commentaire:
Enregistrer un commentaire