4 Replies Latest reply: Sep 18, 2013 8:39 AM by user7972101 RSS

    Unable to programmatically edit a new cell

    894578
      Hi,

      I need to programmatically insert a new row in the table and then programmatically edit certain cell(s) in that row. The edit just after the insert is not working. Could you please advise?

      Thx
      import javafx.application.Application;
      
      import javafx.beans.property.SimpleStringProperty;
      import javafx.beans.value.ChangeListener;
      import javafx.beans.value.ObservableValue;
      
      import javafx.collections.FXCollections;
      import javafx.collections.ObservableList;
      
      import javafx.event.ActionEvent;
      import javafx.event.EventHandler;
      
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.control.ButtonBuilder;
      import javafx.scene.control.TableCell;
      import javafx.scene.control.TableColumn;
      import javafx.scene.control.TableColumnBuilder;
      import javafx.scene.control.TableView;
      import javafx.scene.control.TableViewBuilder;
      import javafx.scene.control.TextField;
      import javafx.scene.control.TextFieldBuilder;
      import javafx.scene.control.cell.PropertyValueFactory;
      import javafx.scene.layout.VBox;
      import javafx.scene.layout.VBoxBuilder;
      
      import javafx.stage.Stage;
      
      import javafx.util.Callback;
      
      public class ProgramaticEditTableViewApplication extends Application {
      
          public ProgramaticEditTableViewApplication() {
          }
      
      
          public static void main(String[] args) {
              launch(args);
          }
      
          @Override
          public void start(Stage stage) throws Exception {
               ObservableList<Person> data = FXCollections.observableArrayList(new Person("John"));
              final TableColumn<Person, String> firstNameCol = TableColumnBuilder.<Person, String>create().text("First Name").cellValueFactory(new PropertyValueFactory<Person, String>("firstName")).minWidth(100).build();
              firstNameCol.setCellFactory(new Callback<TableColumn<Person, String>, TableCell<Person, String>>() {
      
                      @Override
                      public TableCell<Person, String> call(TableColumn<Person, String> tablecolumn) {
                          return new EditingCell();
                      }
                  });
      
      
              final TableView<Person> table = TableViewBuilder.<Person>create().editable(true).items(data).build();
              table.getColumns().addAll(firstNameCol);
      
              Button addAndEditBtn = ButtonBuilder.create().text("Add&Edit").onAction(new EventHandler<ActionEvent>() {
      
                          @Override
                          public void handle(ActionEvent actionEvent) {
                              Person newPerson = new Person("new");
                              table.getItems().add(newPerson);
                              table.edit(0, firstNameCol);
      
                          }
                      }).build();
      
              VBox root = VBoxBuilder.create().children(table, addAndEditBtn).build();
              Scene scene = new Scene(root, 400, 400);
              stage.setScene(scene);
              stage.show();
          }
      
      
          class Person {
      
              private final SimpleStringProperty firstName;
      
      
              private Person(String fName) {
                  this.firstName = new SimpleStringProperty(fName);
              }
      
      
              public String getFirstName() {
                  return firstName.get();
              }
      
              public void setFirstName(String fName) {
                  firstName.set(fName);
              }
      
          }
          
          class EditingCell extends TableCell<Person, String> {
      
              private TextField textField;
      
      
              public EditingCell() {
              }
      
      
              @Override
              public void startEdit() {
                  super.startEdit();
                  createTextField();
                  setText(null);
                  setGraphic(textField);
                  textField.selectAll();
              }
      
              @Override
              public void cancelEdit() {
                  super.cancelEdit();
                  setText((String) getItem());
                  setGraphic(null);
              }
      
              @Override
              public void updateItem(String item, boolean empty) {
                  super.updateItem(item, empty);
      
                  if (empty) {
                      setText(null);
                      setGraphic(null);
                  } else {
                      if (isEditing()) {
                          if (textField != null) {
                              textField.setText(getString());
                          }
                          setText(null);
                          setGraphic(textField);
                      } else {
                          setText(getString());
                          setGraphic(null);
                      }
                  }
              }
      
              private TextField createTextField() {
                  if (textField == null) {
                      textField = TextFieldBuilder.create().text(getString()).minWidth(this.getWidth() - (this.getGraphicTextGap() * 2)).build(); 
                      textField.focusedProperty().addListener(new ChangeListener<Boolean>() {
                              @Override
                              public void changed(ObservableValue<? extends Boolean> observableValue, Boolean arg1, Boolean arg2) {
                                  if (!arg2) {
                                      commitEdit(textField.getText());
                                  }
                              }
                          });
                  }
                  return textField;
              }
      
              private String getString() {
                  return (getItem() == null) ? "" : getItem().toString();
              }
          }
      }
      Thx,

      Edited by: 891575 on Aug 31, 2012 9:11 AM

      Edited by: 891575 on Sep 3, 2012 4:11 AM
        • 1. Re: Unable to programmatically editing a new cell
          shakir.gusaroff
          Hi. Try data.add(newPerson) instead of table.getItems().add(newPerson);
          • 2. Re: Unable to programmatically edit a new cell
            894578
            Hi,

            I tried nothing change

            Let me re-explain my issue:

            I have a table control and an "Add&Edit" button. When the user clicks on the "Add&Edit" button I need to add a new empty row to the table then changing the state of the new added cell to editable(showing a textarea and giving the user the ability to directly type text)
            The add is working fine but the table.edit(...) call just after the add is not working.
            Note that if the table.edit(...) isn't called just after the add it is working fine

            Any idea why the edit is not working if I call it just after the add:
            data.add(newPerson);
            table.edit(0, firstNameCol);
            Thx

            Edited by: 891575 on Sep 3, 2012 4:12 AM
            • 3. Re: Unable to programmatically edit a new cell
              drenda81
              I have the same issue. Anyone has some ideas to resolve the problem?

              Thanks very much!
              • 4. Re: Unable to programmatically edit a new cell
                user7972101

                In my case its working "sometimes", if I put the code to open the cell editor in a Platform.runLater as in the sample code below. Note, that at least in my case, I also had to make the row visible using scrollTo... in which the cell editor shall be opened:

                  
                   TableColumn<Object, ?> myColumn = ....;
                   int myRow = ...;
                   table.scrollTo(myRow);
                   Platform.runLater(new Runnable() {
                   @Override
                   public void run() {
                      table.edit(myRow, myColumn);
                   }
                });