1

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

Open here to see table view

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

6
  • @alexvaluiskyi no ... whatever the problem, refresh is not the correct solution (with a probability to extremely near to 1)! Commented Jul 21, 2019 at 8:27
  • hmm ... why do you expect the value to be updated on keyReleased? Edit, press enter and the value is committed. Commented Jul 21, 2019 at 8:28
  • @kleopatra It seems you had solution for this challenge ? Could you help me with some code in detail. I really appreciate your help Commented Jul 21, 2019 at 9:17
  • your code works as expected .. (the only modification was to move the setup into the apps start method and replace the database access with hard-coded values - which you should have done to make it more easily reproducible :) Commented Jul 21, 2019 at 9:21
  • sry, don't understand what you mean .. your while loop looks (and should be)! unrelated to the editing. Anyway, don't call setItems in the loop: instead, collect the data, then set the items when all are collected. And please fix the formatting (while making it a minimal reproducible example to ensure that what you show us is really what is misbehaving and not some snippet produced on-the-fly), it's extremely hard to read Commented Jul 21, 2019 at 12:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.