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

TableView: first row focused after first insert of item

XMLWordPrintable

      This happens at the very first insert after instantiation of the TableView (might be similar for remove). Reason is TableViewFocusModel

      - constructor calls updateDefaultFocus
      - updateDefaultFocus sets isDefaultFocus to true
      - next listChange runs into special case with hard-coded focus to 0

      To solve, either add a boolean parameter to updateDefaultFocus which should set the flag or false the flag in the constructor after calling updateDefaultFocus.

      Also I suspect (untested! just reading the code) that is too sticky: it's set whenever the list is swept out by another list but only reset to false after receiving a real addition of items thereafter. So it seems to sleep through all (other) modifications to the list and all modifications of focus state.Instead, it probably should be reset with any list modification and any change to focus state.
          
          /**
           * To reproduce, run and
           * - select any row (third or so, just to better see the inconsistent update of
           * focused and selected index)
           * - press f1 to insert item at 0
           * - expected: selected and focused index increased by one
           * - actually: selected increased by one, focused on first row
           *
           */
          public class TableFirstInsert extends Application {
              private final ObservableList<Locale> data =
                      FXCollections.observableArrayList(Locale.getAvailableLocales()
                              );
             
              private final TableView<Locale> table = new TableView<>(data);
              
              @Override
              public void start(Stage stage) {
                  stage.setTitle("Table FocusedCell Bug");
                  TableColumn<Locale, String> language = new TableColumn<>(
                          "Language");
                  language.setCellValueFactory(new PropertyValueFactory<>("displayLanguage"));
                  table.setItems(data);
                  table.getColumns().addAll(language);
          
                  table.addEventFilter(KeyEvent.KEY_PRESSED, e -> {
                      if (e.getCode() == KeyCode.F1) {
                          data.add(0, new Locale("dummy"));
                      }
                  });
          
                  BorderPane root = new BorderPane(table);
                  Scene scene = new Scene(root);
                  stage.setScene(scene);
                  stage.setTitle(System.getProperty("java.version"));
                  stage.show();
              }
          
              public static void main(String[] args) {
                  launch(args);
              }
           }

            jgiles Jonathan Giles
            fastegal Jeanette Winzenburg
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: