10 Replies Latest reply: Apr 22, 2013 3:18 PM by eflores76 RSS

    TableView setEditable is not working

    eflores76
      Hello, the method setEditable(boolean value) in TabeView is not working.

      I read the documentation and I'm supposed to use de enter key, but it does not work. I also read in this forum, and I found that a bug was reported, but that forum thread was issued almost a year ago. So I was wondering if it is my javafx version that is giving me the problem.

      Thanks in advance.
        • 1. Re: TableView setEditable is not working
          abhinay_a
          Hi,

          AFAIK, it works, try setting the setEditable(true) for the column as well.

          Regards,
          Abhi
          • 2. Re: TableView setEditable is not working
            eflores76
            thanks for the answer, I will try your advice and let you know.

            Edited by: eflores76 on 21-abr-2013 6:58
            • 3. Re: TableView setEditable is not working
              eflores76
              It does not work. I think it is matter of my javafx version.
              • 4. Re: TableView setEditable is not working
                James_D
                Can you post a simple, executable code sample that fails?

                The following (basically adapted from the [url http://docs.oracle.com/javafx/2/ui_controls/table-view.htm]tutorial) works fine for me:
                import javafx.application.Application;
                import javafx.beans.property.SimpleStringProperty;
                import javafx.beans.property.StringProperty;
                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.TableColumn;
                import javafx.scene.control.TableView;
                import javafx.scene.control.cell.PropertyValueFactory;
                import javafx.scene.control.cell.TextFieldTableCell;
                import javafx.scene.layout.BorderPane;
                import javafx.stage.Stage;
                
                public class EditableTable extends Application {
                
                  @Override
                  public void start(Stage primaryStage) {
                    final BorderPane root = new BorderPane();
                    final TableView<Person> table = new TableView<Person>();
                    table.setItems(createData());
                    final TableColumn<Person, String> firstNameColumn = new TableColumn<>("First Name");
                    final TableColumn<Person, String> lastNameColumn = new TableColumn<>("Last Name");
                    firstNameColumn.setCellValueFactory(new PropertyValueFactory<Person, String>("firstName"));
                    lastNameColumn.setCellValueFactory(new PropertyValueFactory<Person, String>("lastName"));
                    table.getColumns().addAll(firstNameColumn, lastNameColumn);
                
                    table.setEditable(true);
                    firstNameColumn.setEditable(true);
                    lastNameColumn.setEditable(true);
                
                    firstNameColumn.setCellFactory(TextFieldTableCell.<Person> forTableColumn());
                    lastNameColumn.setCellFactory(TextFieldTableCell.<Person> forTableColumn());
                
                    Button infoButton = new Button("Show info");
                    infoButton.setOnAction(new EventHandler<ActionEvent>() {
                      @Override
                      public void handle(ActionEvent event) {
                        for (Person p : table.getItems()) {
                          System.out.printf("%s %s%n", p.getFirstName(), p.getLastName());
                        }
                        System.out.println();
                      }
                    });
                
                    root.setCenter(table);
                    root.setBottom(infoButton);
                    primaryStage.setScene(new Scene(root, 400, 600));
                    primaryStage.show();
                  }
                
                  public static void main(String[] args) {
                    launch(args);
                  }
                
                  private ObservableList<Person> createData() {
                    return FXCollections.observableArrayList(new Person("Hugo", "Lloris"),
                        new Person("Benoit", "Assou-Ekotto"), new Person("Jan", "Vertonghen"),
                        new Person("Michael", "Dawson"), new Person("Kyle", "Walker"),
                        new Person("Scott", "Parker"), new Person("Mousa", "Dembele"),
                        new Person("Gylfi", "Sigurdsson"), new Person("Gareth", "Bale"),
                        new Person("Aaron", "Lennon"), new Person("Jermane", "Defoe"));
                  }
                
                  public static class Person {
                    private final StringProperty firstName;
                    private final StringProperty lastName;
                
                    Person(String firstName, String lastName) {
                      this.firstName = new SimpleStringProperty(this, "firstName", firstName);
                      this.lastName = new SimpleStringProperty(this, "lastName", lastName);
                    }
                
                    public String getFirstName() {
                      return firstName.get();
                    }
                
                    public void setFirstName(String firstName) {
                      this.firstName.set(firstName);
                    }
                
                    public StringProperty firstNameProperty() {
                      return firstName;
                    }
                
                    public String getLastName() {
                      return lastName.get();
                    }
                
                    public void setLastName(String lastName) {
                      this.lastName.set(lastName);
                    }
                
                    public StringProperty lastNameProperty() {
                      return lastName;
                    }
                  }
                }
                • 5. Re: TableView setEditable is not working
                  eflores76
                  Hello James, thanks for your answer.

                  I have used that same code, and it does not work for me. I even tried invoking setEditable on the TableColumns references.
                  And also I have updated JDK to the last version. 1.7.0_21.

                  Look:
                  /*
                   * To change this template, choose Tools | Templates
                   * and open the template in the editor.
                   */
                  package tableviewsample;
                  
                  import javafx.application.Application;
                  import javafx.collections.FXCollections;
                  import javafx.collections.ObservableList;
                  import javafx.event.ActionEvent;
                  import javafx.event.EventHandler;
                  import javafx.geometry.Insets;
                  import javafx.scene.Group;
                  import javafx.scene.Scene;
                  import javafx.scene.control.Button;
                  import javafx.scene.control.Label;
                  import javafx.scene.control.TableColumn;
                  import javafx.scene.control.TableView;
                  import javafx.scene.control.TextField;
                  import javafx.scene.control.cell.PropertyValueFactory;
                  import javafx.scene.layout.HBox;
                  import javafx.scene.layout.VBox;
                  import javafx.scene.text.Font;
                  import javafx.stage.Stage;
                  
                  /**
                   *
                   * @author Eugenio
                   */
                  public class TableViewSample extends Application {
                  
                      private TableView table = new TableView();
                  
                      @Override
                      public void start(Stage stage) {
                          Scene scene = new Scene(new Group());
                          stage.setTitle("Table View Sample");
                          stage.setWidth(400);
                          stage.setHeight(500);
                  
                          final Label label = new Label("Address Book");
                          label.setFont(new Font("Arial", 20));
                  
                  
                          TableColumn firstNameCol = new TableColumn("First Name");
                          TableColumn lastNameCol = new TableColumn("Last Name");
                          TableColumn emailCol = new TableColumn("Email");
                          emailCol.setMinWidth(200);
                          firstNameCol.setEditable(true);
                          lastNameCol.setEditable(true);
                          emailCol.setEditable(true);
                          table.setEditable(true);
                          table.getColumns().addAll(firstNameCol, lastNameCol, emailCol);
                  
                          final ObservableList<Person> data = FXCollections.observableArrayList(
                                  new Person("Jacob", "Smith", "jacob.smith@example.com"),
                                  new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
                                  new Person("Ethan", "Williams", "ethan.williams@example.com"),
                                  new Person("Emma", "Jones", "emma.jones@example.com"),
                                  new Person("Michael", "Brown", "michael.brown@example.com"));
                  
                          firstNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("firstName"));
                          lastNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("lastName"));
                          emailCol.setCellValueFactory(new PropertyValueFactory<Person, String>("email"));
                          table.setItems(data);
                  
                          final TextField addFirstName = new TextField();
                          addFirstName.setPromptText("First Name");
                          addFirstName.setMaxWidth(firstNameCol.getPrefWidth());
                          final TextField addLastName = new TextField();
                          addLastName.setMaxWidth(lastNameCol.getPrefWidth());
                          addLastName.setPromptText("Last Name");
                          final TextField addEmail = new TextField();
                          addEmail.setMaxWidth(emailCol.getPrefWidth());
                          addEmail.setPromptText("Email");
                          final Button addButton = new Button("Add");
                          addButton.setOnAction(new EventHandler<ActionEvent>() {
                              @Override
                              public void handle(ActionEvent e) {
                                  data.add(new Person(
                                          addFirstName.getText(),
                                          addLastName.getText(),
                                          addEmail.getText()));
                                  addFirstName.clear();
                                  addLastName.clear();
                                  addEmail.clear();
                              }
                          });
                  
                          final HBox hb = new HBox();
                          hb.getChildren().addAll(addFirstName, addLastName, addEmail, addButton);
                          hb.setSpacing(3);
                  
                          final VBox vbox = new VBox();
                          vbox.setSpacing(5);
                          vbox.setPadding(new Insets(10, 0, 0, 10));
                          vbox.getChildren().addAll(label, table, hb);
                  
                  
                  
                          ((Group) scene.getRoot()).getChildren().addAll(vbox);
                          stage.setScene(scene);
                          stage.show();
                  
                  
                      }
                  
                      /**
                       * The main() method is ignored in correctly deployed JavaFX application.
                       * main() serves only as fallback in case the application can not be
                       * launched through deployment artifacts, e.g., in IDEs with limited FX
                       * support. NetBeans ignores main().
                       *
                       * @param args the command line arguments
                       */
                      public static void main(String[] args) {
                          launch(args);
                      }
                  }
                  • 6. Re: TableView setEditable is not working
                    James_D
                    eflores76 wrote:
                    Hello James, thanks for your answer.

                    I have used that same code, and it does not work for me.
                    So what happens? If you double-click a cell, you should see a text field. Click in the text field and you should be able to edit the data. Does none of that happen?
                    I even tried invoking setEditable on the TableColumns references.
                    That's already there:
                    firstNameColumn.setEditable(true);
                    lastNameColumn.setEditable(true);
                    And also I have updated JDK to the last version. 1.7.0_21.

                    Look:
                    Your code sample doesn't set the cellFactory on the columns, so no TextField will appear.
                    • 7. Re: TableView setEditable is not working
                      shakir.gusaroff
                      Your example is for adding a new row, not for update.
                      For update try the following:

                      Example 13-10 TableViewSample with Enabled Cell Editing
                      http://docs.oracle.com/javafx/2/ui_controls/table-view.htm#CJAGAAEE

                      Edited by: shakir.gusaroff on Apr 22, 2013 11:44 AM
                      • 8. Re: TableView setEditable is not working
                        James_D
                        The Person class will need firstNameProperty(), lastNameProperty(), and emailProperty() methods, else the changes made in the editor will not be persisted back to the data objects.

                        But I think your more fundamental problem (currently) is that you're missing the
                        firstNameCol.setCellFactory(TextFieldTableCell.forTableColumn());
                        ...
                        method calls.
                        • 9. Re: TableView setEditable is not working
                          eflores76
                          James, thank you so much for your help. I will make the changes in my code.

                          Thanks again.
                          • 10. Re: TableView setEditable is not working
                            eflores76
                            Shakir, thanks for your answer.