Value is not updated on cell of Table View after editing
I've used java to write program. I tried to edit cell of table view but It was not updated after key released.
Here is this case when I want to edit any cell at Tour No. Column
Main class
ObservableList<Tours> tourdata=FXCollections.observableArrayList();
public void Halongbay()
{
TableView<Tours> table = new TableView<Tours>();
table.setEditable(true);
// Tạo cột ProjectID (Kiểu dữ liệu String)
TableColumn<Tours, String> hotelCol = new TableColumn<Tours, String>("Hotel");
TableColumn<Tours, String> priceCol = new TableColumn<Tours, String>("Price per Day ");
TableColumn<Tours, String> tournumberCol = new TableColumn<Tours, String>("Tour No.");
hotelCol.setCellValueFactory(new PropertyValueFactory<>("hotelname"));
priceCol.setCellValueFactory(new PropertyValueFactory<>("price"));
tournumberCol.setCellValueFactory(new PropertyValueFactory<>("tournumber"));
tournumberCol.setEditable(true);
tournumberCol.setCellFactory(TextFieldTableCell.<Tours>forTableColumn());
tournumberCol.setOnEditCommit(
(TableColumn.CellEditEvent<Tours, String> t) ->
(t.getTableView().getItems().get(t.getTablePosition().getRow())).setTournumber(t.getNewValue()));
table.getColumns().addAll(hotelCol,priceCol,tournumberCol);
String connectionUrl = "jdbc:sqlserver://"+BuildIP+":"+TCP+";instance="+Instancename+";databaseName="+Db+";user="+UID+";password="+PSW+"";
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try{
con = DriverManager.getConnection(connectionUrl);
String sql = "SELECT *from INFOR ";
stmt = con.prepareStatement(sql);
rs=stmt.executeQuery();
while(rs.next()) {
tourdata.add(new Tours
(rs.getString(1),
rs.getString(2),
"10"
));
table.setItems(tourdata);
}
stmt.close();
rs.close();
}
catch(Exception ex){System.err.println(ex);}
VBox root = new VBox();
root.setPadding(new Insets(5));
root.setAlignment(Pos.CENTER);
root.setSpacing(20);
root.getChildren().addAll(table);
Stage stage = new Stage();
stage.setTitle("Tourist information");
Scene scene = new Scene(root, 700, 500);
stage.setScene(scene);
stage.show();
}
Tours Class
public class Tours {
private String hotelname;
private String price;
private String tournumber;
public Tours(String hotelname, String price, String tournumber) {
super();
this.hotelname = hotelname;
this.price = price;
this.tournumber = tournumber;
}
public String getHotelname() {
return hotelname;
}
public void setHotelname(String hotelname) {
this.hotelname = hotelname;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getTournumber() {
return tournumber;
}
public void setTournumber(String tournumber) {
this.tournumber = tournumber;
}
I expect any cell at Tour No. can be edited and show new value on table view. Thanks for help