JavaFX - how to set active control by default

I have one JavaFX application window created from one JavaFX tutorial.

I install new windowed content using the following function:

private Initializable replaceSceneContent(final String fxml) throws Exception { // wczytanie fxml FXMLLoader loader = new FXMLLoader(); InputStream in = Main.class.getResourceAsStream(fxml); loader.setBuilderFactory(new JavaFXBuilderFactory()); loader.setLocation(Main.class.getResource(fxml)); AnchorPane page; try { page = (AnchorPane) loader.load(in); } finally { in.close(); } Scene scene = new Scene(page, w, h); stage.setScene(scene); return (Initializable) loader.getController(); } 

But I want to select one of the TextFields from this fxml file, which will be active by default. How to do it? I tried calling the requestFocus method in the controller initialization method, but it did not work. I did not find a suitable property in the TextField class or in the AnchorPane class (AnchorPane is the root element of the fxml control tree).

+4
java javafx javafx-2
Nov 12
source share
1 answer

Try wrapping the requestFocus() call with PlatForm.runlater

 Platform.runLater(new Runnable() { public void run() { textField.requestFocus(); } }); 
+9
Nov 12
source share



All Articles