title: First Row Always Gets Selected When Focussing a TableView or TreeView in a JavaFX Application
body: <p>I realised that all tableViews and treeViews in my JavaFX application are selecting the first row as soon as they get focused. As a result even when I click on a corresponding scrollbar, the first row will be selected. Since I of course do not want to have such a behaviour, I would like to to ask, if there is a way to avoid this behaviour?</p>
  
  <p>EDIT: I added a simple example that creats two humble lists. Here as soon as one clicks on an empty row, the first row gets selected.</p>
  
  <p>EDIT 2: Added two small listener to the code, so that the problem is repeatable without restarting the application.</p>
  
  <pre><code>package testlistview;
  
  import javafx.application.Application;
  import javafx.scene.Scene;
  import javafx.scene.control.ListView;
  import javafx.scene.input.MouseEvent;
  import javafx.scene.layout.HBox;
  import javafx.stage.Stage;
  
  public class TestListView extends Application {
  
      public static void main(String[] args) {
          launch(args);
      }
  
      @Override
      public void start(Stage primaryStage) throws Exception {
          HBox root = new HBox();
          root.setSpacing(10);
  
          Scene primaryStageScene = new Scene(root);
          primaryStage.setScene(primaryStageScene);
  
          ListView&lt;String&gt; l1 = new ListView();
          l1.getItems().addAll("a", "b", "c", "d", "e", "f", "g");
          ListView&lt;String&gt; l2 = new ListView();
          l2.getItems().addAll("1", "2", "3", "4", "5", "6", "7");
          root.getChildren().addAll(l1, l2);
  
          l1.setOnMouseClicked((MouseEvent e) -&gt; {
              l2.getSelectionModel().clearSelection();
          });
  
          l2.setOnMouseClicked((MouseEvent e) -&gt; {
              l1.getSelectionModel().clearSelection();
          });
  
          primaryStage.show();
      }
  }
  </code></pre>
  
  <p>EDIT 3: I realised that the first row also gets selected, when I am sorting a table. However, to avoid this, I created a little hack (which is probably a very bad style, but it works for now).  </p>
  
  <pre><code>  private boolean sorted = false;
  
  // (...)
  
      tableView.addEventFilter(MouseEvent.MOUSE_PRESSED,
                              new EventHandler&lt;MouseEvent&gt;() {
                                  public void handle(MouseEvent e) {
                                      if (!tableView.isFocused()) {
                                          if (e.getPickResult().getIntersectedNode() instanceof Label) {
                                              sorted = true;
                                          }
  
                                      }
                                  }
                              ;
                      });
  
      tableView.focusedProperty().addListener((a,b,c) -&gt; {
                  if(!tableView.isPressed() || sorted) {
                      tableView.getSelectionModel().clearSelection();
                      sorted = false;
                  }
              });
  </code></pre>
  
  <p>So here the eventFilter is activated before the listener. As far as I can see, only the header is of instance Label. As a result each time the header is clicked first, the selection gets cleared.</p>
  
  <p>However, as discussed in the post below, I guess, the bug will disappear with the next java release.</p>