title: How to get focus on TextField within TabPane, when Enter Key is pressed (using Javafx)?
body: <p>How to requestFocus on TextField of different Tabs of TabPane, whenever the Enter Key is pressed on that particular Tab.</p>
  
  <p>Each Tab within TabPane contains TextField, whenever a particular Tab is selected using Enter Key, than the TextField within that Tab must get Focused (requestFocus)</p>
  
  <pre><code>package tabdemo;
  
  import javafx.application.Application;
  import javafx.beans.value.ChangeListener;
  import javafx.beans.value.ObservableValue;
  import javafx.event.EventHandler;
  import javafx.scene.Scene;
  import javafx.scene.control.Tab;
  import javafx.scene.control.TabPane;
  import javafx.scene.control.TextField;
  import javafx.scene.input.KeyCode;
  import javafx.scene.input.KeyEvent;
  import javafx.scene.layout.Priority;
  import javafx.scene.layout.StackPane;
  import javafx.scene.layout.VBox;
  import javafx.stage.Stage;
  
  
  public class TabDemo extends Application {
  
      @Override
      public void start(Stage primaryStage) {
          TabPane tabPane = new TabPane();
          tabPane.setPrefHeight(500);
          VBox.setVgrow(tabPane, Priority.ALWAYS);
          tabPane.setTabClosingPolicy(TabPane.TabClosingPolicy.UNAVAILABLE);
          tabPane.setStyle("-fx-background-color: #fff; -fx-border-color: #e7eaec #e7eaec #e7eaec #e7eaec; -fx-border-width: 6;");
  
          Tab tab1 = new Tab();
          tab1.setText(" Try Demo");
          tab1.setId("TryDemo");
          TextField username1 = new TextField();
          tab1.setContent(username1);
  
          Tab tab2 = new Tab();
          tab2.setText(" Select Company");
          tab2.setId("SelectCompany");
          TextField username2 = new TextField();
          tab2.setContent(username2);
  
          Tab tab3 = new Tab();
          tab3.setText(" Create Company");
          tab3.setId("CreateCompany");
          TextField username3 = new TextField();
          tab3.setContent(username3);
  
          //Add childen to tabpane
          tabPane.getTabs().addAll(tab1, tab2, tab3);
  
          StackPane root = new StackPane();
          root.getChildren().add(tabPane);
  
          Scene scene = new Scene(root, 300, 250);
  
          primaryStage.setTitle("Hello World!");
          primaryStage.setScene(scene);
          primaryStage.show();
  
          // Tabpane Change Listener
          tabPane.getSelectionModel().selectedItemProperty().addListener(new ChangeListener&lt;Tab&gt;() {
  
              @Override
              public void changed(ObservableValue&lt;? extends Tab&gt; arg0, Tab oldtab, Tab newtab) {
                  if (newtab == tab1) {
  
                  }
                  if (newtab == tab2) {
  
                  }
                  if (newtab == tab3) {
                      //tab3.getContent().requestFocus();
                      //username3.requestFocus();
                  }
              }
          });
          // End Tabpane Change Listener
  
          // Root Listner
          root.setOnKeyPressed(new EventHandler&lt;KeyEvent&gt;() {
  
              public void handle(KeyEvent event) {
                  if (event.getCode() == KeyCode.TAB || event.getCode() == KeyCode.ENTER) {
                      tabPane.requestFocus();
                      event.consume();
                  }
              }
          });
          // END Root Listner
  
      }
  
      /**
       * @param args the command line arguments
       */
      public static void main(String[] args) {
          launch(args);
      }
  
  }
  </code></pre>
  
  <p>Any help would be appreciated.</p>