1 Reply Latest reply: Apr 5, 2013 1:54 PM by James_D RSS

    TableViev TableCell

    1001303
      A table cell cann't be edited successfully when the cell is empty or its value is null.
      When a cell get a null value,any editing will just be ignored when use commitEdit method of the TableCell.
      Why??
        • 1. Re: TableViev TableCell
          James_D
          Not sure what you would want it to do when the cell is empty: there is no corresponding data to set to the result of the edit.

          For a null value in a non-empty cell, isEmpty() appears to incorrectly return true in JavaFX 2.2.7. I guess this is what is causing the issue with failing to edit correctly. In JavaFX 8.0 (prerelease, b82) it seems to be fixed, but I can't find a corresponding JIRA (didn't spend too much time searching, to be honest).

          The following works in FX 8, but fails as you describe in FX 2.2.7:
          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 EditableTableWithNull extends Application {
          
            @Override
            public void start(Stage primaryStage) {
              final BorderPane root = new BorderPane();
              final TableView<Person> table = new TableView<Person>();
              table.setEditable(true);
              table.setItems(createData());
              final TableColumn<Person, String> firstNameColumn = new TableColumn<>("First Name");
              final TableColumn<Person, String> lastNameColumn = new TableColumn<>("Last Name");
              final TableColumn<Person, String> emailColumn = new TableColumn<>("Email");
              firstNameColumn.setCellValueFactory(new PropertyValueFactory<Person, String>("firstName"));
              lastNameColumn.setCellValueFactory(new PropertyValueFactory<Person, String>("lastName"));
              emailColumn.setCellValueFactory(new PropertyValueFactory<Person, String>("email"));
              emailColumn.setEditable(true);
              emailColumn.setMinWidth(120);
              emailColumn.setCellFactory(TextFieldTableCell.<Person>forTableColumn());
              
              table.getColumns().addAll(firstNameColumn, lastNameColumn, emailColumn);
              
              root.setCenter(table);
              
              final Button button = new Button("Dump info");
              button.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event) {
                  for (Person p : table.getItems()) System.out.println(p);
                  System.out.println();
                }
              });
              root.setBottom(button);
              
              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 ;
              private final StringProperty email ;
              Person(String firstName, String lastName, String email) {
                this.firstName = new SimpleStringProperty(this, "firstName", firstName);
                this.lastName = new SimpleStringProperty(this, "lastName", lastName);
                this.email = new SimpleStringProperty(this, "email", email);
              }
              Person(String firstName, String lastName) {
                this(firstName, lastName, null);
              }
              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 ; }  
              public String getEmail() { return email.get() ; }
              public void setEmail(String email)  { this.email.set(email); }
              public StringProperty emailProperty() { return email ; }
              @Override public String toString() { return String.format("%s %s (Email: %s)", firstName.get(), lastName.get(), email.get()); }
              }
          }