title: Is there a way to select a text field in code without clicking on it (javafx)?
body: <p>I am not sure how to or even if it is possible to select text fields in code. I could not find anything on the subject. If the is indeed a way to do so i have a template that could be modified to demonstrate how this could be done.</p>
  
  <p>Here is the template/sample code:</p>
  
  <p>Main class:</p>
  
  <pre><code>    package application;
  
      import javafx.application.Application;
      import javafx.fxml.FXMLLoader;
      import javafx.scene.Parent;
      import javafx.scene.Scene;
      import javafx.stage.Stage;
  
  
      public class Main extends Application {
  public void start(Stage primaryStage) {
      try {
          Parent root = FXMLLoader.load(getClass().getResource("/fxml/Main.fxml"));
          Scene scene = new Scene(root,600,400);
          scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
          primaryStage.setScene(scene);
          primaryStage.setTitle("Test");
          primaryStage.show();
      } catch(Exception e) {
          e.printStackTrace();
      }
  }
  
  public static void main(String[] args) {
      launch(args);
  }
  }
  </code></pre>
  
  <p>MainControl class:</p>
  
  <pre><code>    package application;
  
      import java.net.URL;
      import java.util.ResourceBundle;
  
      import javafx.event.ActionEvent;
      import javafx.fxml.FXML;
      import javafx.fxml.Initializable;
      import javafx.scene.control.Button;
      import javafx.scene.control.TextField;
  
      public class MainControl implements Initializable {
  @FXML
  Button button;
  @FXML
  TextField field1;
  @FXML
  TextField field2;
  boolean field1selected=true;
  public void initialize(URL arg0, ResourceBundle arg1) {
  
  }
  public void switchTextFeild(ActionEvent event){
  /*
  The code for switch on witch TextField is selected would go here
  */
  }
  }
  </code></pre>
  
  <p>Main.fxml:</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
      &lt;?import javafx.scene.text.*?&gt;
      &lt;?import javafx.scene.control.*?&gt;
      &lt;?import java.lang.*?&gt;
      &lt;?import javafx.scene.layout.*?&gt;
  
  
      &lt;AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainControl"&gt;
      &lt;children&gt;
      &lt;TextField fx:id="field1" layoutX="6.0" layoutY="2.0" prefHeight="394.0" prefWidth="149.0" /&gt;
      &lt;TextField fx:id="field2" layoutX="445.0" layoutY="2.0" prefHeight="394.0" prefWidth="149.0" /&gt;
    &lt;Button fx:id="button" layoutX="177.0" layoutY="14.0" mnemonicParsing="false" onAction="#switchTextFeild" prefHeight="95.0" prefWidth="239.0" text="Switch" textAlignment="CENTER" wrapText="true"&gt;
       &lt;font&gt;
          &lt;Font size="24.0" /&gt;
       &lt;/font&gt;
    &lt;/Button&gt;
    &lt;/children&gt;
    &lt;/AnchorPane&gt;
  </code></pre>