4 Replies Latest reply on Sep 18, 2013 1:39 PM by tomkrauss

    Unable to programmatically edit a new cell

    elisad
      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