Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8089610

[TableView] TableView Cell Background not set correctly after CancelEdit

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open
    • Priority: P4
    • Resolution: Unresolved
    • Affects Version/s: 8u5, 8u20
    • Fix Version/s: tbd
    • Component/s: javafx
    • Environment:

      Mac OSX 10.9

      Description

      When editing cells on a TableView the cell background is only reset to "selected cell" when calling commitEdit.

      So when cancel edit with Cell.cancelEdit the background color will stay gray.
      Here is a quick sample code

      public class FXMLDocumentController implements Initializable {

          @FXML
          private TableView tableView;
          @FXML
          private TableColumn<TestItem, String> c1;
          @FXML
          private TableColumn<TestItem, String> c2;

          @Override
          public void initialize(URL url, ResourceBundle rb) {
              final ObservableList<TestItem> data = FXCollections.observableArrayList(
                      new TestItem(), new TestItem(), new TestItem(), new TestItem());
              tableView.getSelectionModel().setCellSelectionEnabled(true);
              tableView.setEditable(true);
              tableView.itemsProperty().set(data);

              tableView.setOnKeyReleased((KeyEvent t) -> {
                  if (t.getCode() == KeyCode.ENTER) {
                      if (!tableView.isEditable()) {
                          tableView.setEditable(true);
                          ObservableList<TablePosition> list = tableView.getSelectionModel().getSelectedCells();
                          list.get(0).getTableColumn().setEditable(true);
                          tableView.edit(list.get(0).getRow(), list.get(0).getTableColumn());
                      }
                  }
              });
              c1.setCellValueFactory(
                      new PropertyValueFactory<TestItem, String>("c1")
              );
              c2.setCellValueFactory(
                      new PropertyValueFactory<TestItem, String>("c2")
              );

              c1.setCellFactory((TableColumn<TestItem, String> p) -> {
                  final TableCell<TestItem, String> cell = new TextFieldCell();
                  // cell.selectedProperty().addListener(new CellSelectionChangeListener<>(cell));

                  return cell;
              });
              tableView.setOnMouseClicked((MouseEvent t) -> {
                  ObservableList<TablePosition> list = tableView.getSelectionModel().getSelectedCells();
                  list.get(0).getTableColumn().setEditable(true);

              });

              tableView.getSelectionModel().clearAndSelect(0, c1);

              tableView.setOnMouseClicked((MouseEvent t) -> {
                  ObservableList<TablePosition> list = tableView.getSelectionModel().getSelectedCells();
                  list.get(0).getTableColumn().setEditable(true);
                  tableView.edit(list.get(0).getRow(), list.get(0).getTableColumn());
              });
          }

          private static class TestItem {

              private final SimpleStringProperty c1;
              private final SimpleStringProperty c2;

              public TestItem() {
                  this.c1 = new SimpleStringProperty("C1");
                  this.c2 = new SimpleStringProperty("C2");
              }
          }

          final class TextFieldCell extends TableCell<TestItem, String> {

              private final TextField textField;
              // private FileOverviewFile file;

              public TextFieldCell() {
                  textField = new TextField();
                  textField.focusedProperty().addListener((ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) -> {
                      if (!newValue) {
                          commitEdit(textField.getText());
                      }
                  });
                  textField.setOnKeyPressed((KeyEvent t) -> {
                      if (t.getCode() == KeyCode.ENTER) {
                          t.consume();
                          commitEdit(textField.getText());
                      } else if (t.getCode() == KeyCode.ESCAPE) {
                          t.consume();
                          cancelEdit();
                          this.setContentDisplay(ContentDisplay.TEXT_ONLY);
                      }
                  });
                  textField.setDisable(true);
                  this.setGraphic(textField);
                  this.setContentDisplay(ContentDisplay.TEXT_ONLY);
                  this.setEditable(true);
              }

              @Override
              public void startEdit() {
                  super.startEdit();
                  if (isEmpty()) {
                      return;
                  }
                  ObservableValue<String> ov = getTableColumn().getCellObservableValue(getIndex());
                  SimpleStringProperty sp = (SimpleStringProperty) ov;
                  if (!isEmpty()) {
                      textField.textProperty().set("");
                  }
                  this.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
                  textField.setDisable(false);
                  textField.requestFocus();

              }

              @Override
              public void cancelEdit() {
                  super.cancelEdit();
                  textField.setDisable(true);
                  this.setContentDisplay(ContentDisplay.TEXT_ONLY);
                  this.requestFocus();
                  this.getTableView().getSelectionModel().clearAndSelect(this.getIndex() + 1, this.getTableColumn());
              }

              @Override
              public void commitEdit(String value) {

                  textField.setDisable(true);
                  this.setContentDisplay(ContentDisplay.TEXT_ONLY);
                  super.commitEdit(value);
                  this.getTableView().getSelectionModel().clearAndSelect(this.getIndex() + 1, this.getTableColumn());

              }
          }

      public class FXMLDocumentController implements Initializable {

          @FXML
          private TableView tableView;
          @FXML
          private TableColumn<TestItem, String> c1;
          @FXML
          private TableColumn<TestItem, String> c2;

          @Override
          public void initialize(URL url, ResourceBundle rb) {
              final ObservableList<TestItem> data = FXCollections.observableArrayList(
                      new TestItem(), new TestItem(), new TestItem(), new TestItem());
              tableView.getSelectionModel().setCellSelectionEnabled(true);
              tableView.setEditable(true);
              tableView.itemsProperty().set(data);

              tableView.setOnKeyReleased((KeyEvent t) -> {
                  if (t.getCode() == KeyCode.ENTER) {
                      if (!tableView.isEditable()) {
                          tableView.setEditable(true);
                          ObservableList<TablePosition> list = tableView.getSelectionModel().getSelectedCells();
                          list.get(0).getTableColumn().setEditable(true);
                          tableView.edit(list.get(0).getRow(), list.get(0).getTableColumn());
                      }
                  }
              });
              c1.setCellValueFactory(
                      new PropertyValueFactory<TestItem, String>("c1")
              );
              c2.setCellValueFactory(
                      new PropertyValueFactory<TestItem, String>("c2")
              );

              c1.setCellFactory((TableColumn<TestItem, String> p) -> {
                  final TableCell<TestItem, String> cell = new TextFieldCell();
                  // cell.selectedProperty().addListener(new CellSelectionChangeListener<>(cell));

                  return cell;
              });
              tableView.setOnMouseClicked((MouseEvent t) -> {
                  ObservableList<TablePosition> list = tableView.getSelectionModel().getSelectedCells();
                  list.get(0).getTableColumn().setEditable(true);

              });

              tableView.getSelectionModel().clearAndSelect(0, c1);

              tableView.setOnMouseClicked((MouseEvent t) -> {
                  ObservableList<TablePosition> list = tableView.getSelectionModel().getSelectedCells();
                  list.get(0).getTableColumn().setEditable(true);
                  tableView.edit(list.get(0).getRow(), list.get(0).getTableColumn());
              });
          }

          private static class TestItem {

              private final SimpleStringProperty c1;
              private final SimpleStringProperty c2;

              public TestItem() {
                  this.c1 = new SimpleStringProperty("C1");
                  this.c2 = new SimpleStringProperty("C2");
              }
          }

          final class TextFieldCell extends TableCell<TestItem, String> {

              private final TextField textField;
              // private FileOverviewFile file;

              public TextFieldCell() {
                  textField = new TextField();
                  textField.focusedProperty().addListener((ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) -> {
                      if (!newValue) {
                          commitEdit(textField.getText());
                      }
                  });
                  textField.setOnKeyPressed((KeyEvent t) -> {
                      if (t.getCode() == KeyCode.ENTER) {
                          t.consume();
                          commitEdit(textField.getText());
                      } else if (t.getCode() == KeyCode.ESCAPE) {
                          t.consume();
                          cancelEdit();
                          this.setContentDisplay(ContentDisplay.TEXT_ONLY);
                      }
                  });
                  textField.setDisable(true);
                  this.setGraphic(textField);
                  this.setContentDisplay(ContentDisplay.TEXT_ONLY);
                  this.setEditable(true);
              }

              @Override
              public void startEdit() {
                  super.startEdit();
                  if (isEmpty()) {
                      return;
                  }
                  ObservableValue<String> ov = getTableColumn().getCellObservableValue(getIndex());
                  SimpleStringProperty sp = (SimpleStringProperty) ov;
                  if (!isEmpty()) {
                      textField.textProperty().set("");
                  }
                  this.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
                  textField.setDisable(false);
                  textField.requestFocus();

              }

              @Override
              public void cancelEdit() {
                  super.cancelEdit();
                  textField.setDisable(true);
                  this.setContentDisplay(ContentDisplay.TEXT_ONLY);
                  this.requestFocus();
                  this.getTableView().getSelectionModel().clearAndSelect(this.getIndex() + 1, this.getTableColumn());
              }

              @Override
              public void commitEdit(String value) {

                  textField.setDisable(true);
                  this.setContentDisplay(ContentDisplay.TEXT_ONLY);
                  super.commitEdit(value);
                  this.getTableView().getSelectionModel().clearAndSelect(this.getIndex() + 1, this.getTableColumn());

              }
          }

      public class TableViewTest extends Application {
          
          @Override
          public void start(Stage stage) throws Exception {
              Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
              
              Scene scene = new Scene(root);
              
              stage.setScene(scene);
              stage.show();
          }

          /**
           * @param args the command line arguments
           */
          public static void main(String[] args) {
              launch(args);
          }
          
      }

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            usengewaljfx Uwe Sengewald (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated:
              Imported: