Don't load the FXML twice like that. You can load multiple times the same .fxml document (multiple scene graph / controllers) but if you want to do together loading the scene graph and initializing the controller you have to call the fxml loader only once.
Here is an example
FXMLLoader loader = new FXMLLoader(getClass().getResource("ServerConfigChooser.fxml"));
ServerConfigChooser controller = new ServerConfigChooser();
loader.setController(controller);
loader.setRoot(controller);
Parent root;
try {
root = (Parent) loader.load();
Scene scene = new Scene(root, 320, 200);
Stage stage = new Stage();
stage.setScene(scene);
stage.show();
} catch (IOException ex) {
Logger.getLogger(ServerConfigChooser.class.getName()).log(Level.SEVERE, null, ex);
}
Note that
- your controller should extend the node type of the root node of your .fxml document
- your .fxml document should use fxroot construct, see this doc (you can set this in scene builder)
- you should remove the controller from the fxml root element. It will conflict with this way of using the
FXMLLoader
class
For example the controller class
public class ServerConfigChooser extends AnchorPane implements Initializable {
...
}
And the .fxml
<fx:root type="javafx.scene.layout.AnchorPane" id="AnchorPane" prefHeight="200.0" prefWidth="320.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2">