title: How to focus a specific node when selected tab changes in JavaFX?
body: <p>I want to set focus to a specific node in the content of a Tab. I added a <strong>ChangeListener</strong> to <strong>selectedItem</strong> property as follows (assume that the class contains a field named <strong>secondNode</strong> of type <strong>Node</strong>):</p>
  
  <pre><code>tabPane.getSelectionModel().selectedItemProperty().addListener(new ChangeListener&lt;Tab&gt;() {
      @Override
      public void changed(ObservableValue&lt;? extends Tab&gt; observable, Tab oldValue, Tab newValue) {
          if (newValue != null) {
              Platform.runLater(new Runnable() {
                  @Override
                  public void run() {
                      secondNode.requestFocus();
                  }
              });
          }
      }
  });
  </code></pre>
  
  <p>However, this does not work. I assume the reason is because TabPane performs some additional actions, which affect focus, after the new tab has been selected (but, looking through <a href="http://hg.openjdk.java.net/openjfx/2.2/master/rt/file/e71070b0e0c0/javafx-ui-controls/src/javafx/scene/control/TabPane.java" rel="nofollow">TabPane source code</a>, I can't figure out what). If I single-step through this code in a debugger, it works as expected, so it appears to be a race condition. If this is so, how can this be resolved?</p>