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

ChangeListener for TableView.focusWithinProperty() is not always called

XMLWordPrintable

    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      ChangeListener for TableView.focusWithinProperty() is not always called. It seems that it depends on the row selected.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the provided code. Click alternately on the right and left tables. You will notice that if you click on the rows below, the ChangeListener is triggered. If you click on the rows above, the ChangeListener is not triggered.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      ChangeListener for TableView.focusWithinProperty() must be always called when table gets focus.
      ACTUAL -
      Please, see https://i.stack.imgur.com/IH4ba.gif

      ---------- BEGIN SOURCE ----------
      public class JavaFxTest7 extends Application {

          private static class Student {

              private int id;

              public Student(int id) {
                  this.id = id;
              }

              public int getId() {
                  return id;
              }
          }

          private TableView<Student> leftTable = new TableView<>(FXCollections.observableList(
                  List.of(new Student(1), new Student(2), new Student(3), new Student(4), new Student(5))));

          private TableView<Student> rightTable = new TableView<>(FXCollections.observableList(
                  List.of(new Student(6), new Student(7), new Student(8), new Student(9), new Student(10))));

          public static void main(String[] args) {
              launch(args);
          }

          @Override
          public void start(Stage primaryStage) {
              var leftColumn = new TableColumn<Student, Integer>();
              leftColumn.setCellValueFactory((data) -> new ReadOnlyObjectWrapper<>(data.getValue().getId()));
              leftTable.getColumns().add(leftColumn);
              leftTable.focusWithinProperty().addListener((ov, oldV, newV) -> {
                  System.out.println("left:" + oldV + " -> " + newV);
              });

              var rightColumn = new TableColumn<Student, Integer>();
              rightColumn.setCellValueFactory((data) -> new ReadOnlyObjectWrapper<>(data.getValue().getId()));
              rightTable.getColumns().add(rightColumn);
              rightTable.focusWithinProperty().addListener((ov, oldV, newV) -> {
                  System.out.println("right:" + oldV + " -> " + newV);
              });
              HBox root = new HBox(leftTable, rightTable);
              var scene = new Scene(root, 400, 300);
              primaryStage.setScene(scene);
              primaryStage.show();
          }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      As focusWithinProperty for the parent node of the TableView will not work use TableView.focusedProperty instead

      NOTES:
      1. TAB/shift-TAB key presses also fail to change focusWithin property

            angorya Andy Goryachev
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated: