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

TableView: clearAndSelect != clear + select when cell selection enabled

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 9
    • 8u65
    • javafx
    • x86_64
    • windows_7

      FULL PRODUCT VERSION :
      reproducible with JDK 8u40, 8u65

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      One may expect that TableView.clearAndSelect() behaves exactly like TableView.clearSelection() followed by TableView.select(rownum), but this is unfortunately not the case when cell selection / multiselection is enabled on the table. Moreover TableView.clearAndSelect() has no effect on such table, where clear + select perform like expected. See the test case.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1) Start the test case
      2) select "Author3" and "Remark4"
      3) press button "clearAndSelect(0)"

      Expected: first line is selected alone
      Result: no change in selection

      The second button ("clear && select(0)") performs like expected. According to java doc and common sense the actions should be equivalent.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      first line is selected alone
      ACTUAL -
      no change in selection

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      Book.java:
      package de.lexcom.tableview;

      import javafx.beans.property.SimpleStringProperty;

      public class Book {
          private SimpleStringProperty title = new SimpleStringProperty();
          private SimpleStringProperty author = new SimpleStringProperty();
          private SimpleStringProperty remark = new SimpleStringProperty();

          public Book(String title, String author, String remark) {
              super();
              setTitle(title);
              setAuthor(author);
              setRemark(remark);
          }

          public SimpleStringProperty titleProperty() {
              return this.title;
          }

          public java.lang.String getTitle() {
              return this.titleProperty().get();
          }

          public void setTitle(final java.lang.String title) {
              this.titleProperty().set(title);
          }

          public SimpleStringProperty authorProperty() {
              return this.author;
          }

          public java.lang.String getAuthor() {
              return this.authorProperty().get();
          }

          public void setAuthor(final java.lang.String author) {
              this.authorProperty().set(author);
          }

          public SimpleStringProperty remarkProperty() {
              return this.remark;
          }

          public java.lang.String getRemark() {
              return this.remarkProperty().get();
          }

          public void setRemark(final java.lang.String remark) {
              this.remarkProperty().set(remark);
          }

          @Override
          public String toString() {
              return String.format("%s(%s) - %s", getTitle(), getAuthor(), getRemark());
          }
      }




      FXTableViewExample2.java:
      package de.lexcom.tableview;

      import javafx.application.Application;
      import javafx.collections.FXCollections;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.control.SelectionMode;
      import javafx.scene.control.TableColumn;
      import javafx.scene.control.TableView;
      import javafx.scene.control.ToolBar;
      import javafx.scene.control.cell.PropertyValueFactory;
      import javafx.scene.layout.VBox;
      import javafx.stage.Stage;

      public class FXTableViewExample2 extends Application {
          public static void main(String [] args) {
              Application.launch(args);
          }

          private TableView<Book> table;

          @Override
          public void start(Stage primaryStage) {

              primaryStage.setTitle("Table View Example 1");

              Button select1 = new Button("clearAndSelect(0)");
              Button select2 = new Button("clear && select(0)");
              ToolBar toolbar = new ToolBar(select1, select2);

              // Table view, data, columns and properties
              table = new TableView<>();
              Book books[] = {
                      new Book("Book 1", "Author 1", "Remark 1")
                      , new Book("Book 2", "Author 2", "Remark 2")
                      , new Book("Book 3", "Author 3", "Remark 3")
                      , new Book("Book 4", "Author 4", "Remark 4")
              };
              table.setItems(FXCollections.observableArrayList());
              table.getItems().addAll(books);

              String[] columns = {
                  "title", "author", "remark"
              };
              for (String prop : columns) {
                  TableColumn<Book, String> col = new TableColumn<>(prop);
                  col.setCellValueFactory(new PropertyValueFactory<>(prop));
                  table.getColumns().add(col);
              }
              table.setColumnResizePolicy(TableView.UNCONSTRAINED_RESIZE_POLICY);
              table.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
              table.getSelectionModel().setCellSelectionEnabled(true);

              final VBox vbox = new VBox(20);
              vbox.getChildren().addAll(toolbar, table);

              select1.setOnAction((event) -> {
                  table.getSelectionModel().clearAndSelect(0);
              });
              select2.setOnAction((event) -> {
                  table.getSelectionModel().clearSelection();
                  table.getSelectionModel().select(0);
              });

              // Scene
              Scene scene = new Scene(vbox, 500, 475);
              primaryStage.setScene(scene);
              primaryStage.show();
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Use .clearSelection() followed by .select(...)

            jgiles Jonathan Giles
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: