My first post, be gentle please.
I am making a JavaFX GUI for a simple game. I have an FXML layout loaded via FXML file loader here:
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
Then I create a button in Main.java:
Button button = new Button("Jp", view);
And in order to display:
primaryStage.setScene(new Scene(root));
primaryStage.show();
Question: How on earth I can combine FXML layout and plain java Button object to be on the same scene ? Is it like apples and oranges, impossible to combine ? My IDE is not letting me invoke the method recomended from java docs:
root.getChildren().add();
and so on...
Please help me and/or point me in some direction, I really tried...
The getChildren()
method defined in Parent
is a protected
method: therefore you cannot access it from your Main
class, which is why your code won't compile.
Pane
overrides this method and makes it public
, so assuming the root element of your FXML file is a Pane
(or subclass of Pane
), you just need to be more specific about the type of root. In other words, again assuming your FXML is as I expect, change
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
to
Pane root = FXMLLoader.load(getClass().getResource("sample.fxml"));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.