Reputation: 262
I have create a script that will create and populate data to tableview (i've used scene builder for tableview). it's column is dynamic depending on the number of columns of mysql table. But I can't find any solutions on how to edit/insert data to tableview and automatically update the MySQL table.
here is my code for dynamic columns and populating data from database to it. By the way, database table I am using has 50 columns.
data = FXCollections.observableArrayList();
try {
c = DBConnect.connect();
//SQL FOR SELECTING ALL OF CUSTOMER
String SQL = "SELECT * from CAM_ODS_TIMESHEET_RAW_MOD";
//ResultSet
ResultSet rs = c.createStatement().executeQuery(SQL);
/**
* ********************************
* TABLE COLUMN ADDED DYNAMICALLY *
* ********************************
*/
for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
//We are using non property style for making dynamic table
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 + "] ");
}
/**
* ******************************
* Data added to ObservableList *
* ******************************
*/
while (rs.next()) {
//Iterate Row
ObservableList<String> row = FXCollections.observableArrayList();
for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
//Iterate Column
if (rs.getString(i) == null) {
row.add("");
} else {
row.add(rs.getString(i));
}
}
System.out.println("Row [1] added " + row);
data.add(row);
}
//FINALLY ADDED TO TableView
tableview.setItems(data);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Error on Building Data");
}
I've also seen multiple examples that uses Model like Person class etc but I am wondering how can I use that for multiple columns like 50 columns
Thank you in advance for your help! :)
Upvotes: 0
Views: 1698
Reputation: 179
Sounds like your requirement like do a fx gui of MYSQL table. if you want to do it, you need add a changlistener for the obserbleList and add the SQL insert/update/delete logic into it.
ObservableList.addListener(ListChangeListener<? super E> listener)
Upvotes: 0