I'm quite new to JavaFX and Java in general, but I've experienced something weird. At first, I took a Java course and there we were creating layout for application using SceneBuilder. I've finished the course and I'm trying to write some simple applications on my own. But whenever I look for some solutions for problems on the Internet, everywhere layout is implemented in code, that means: using something like this:
Label secondLabel = new Label("I'm a Label on new Window");
StackPane secondaryLayout = new StackPane();
secondaryLayout.getChildren().add(secondLabel);
So my question is: how I should create layout? Should I stick to the SceneBuilder or code it in Java as apparently everyone does?
There are benefits for using both approaches and you cannot tell in general which one is better without analyzing the scene you're trying to create
GridPane
there is no way to insert multiple nodes to multiple columns per <fx:include>
tag.fx:id
s to all the elements and then listing all of them at a different location.Integer
to the child list of a parent? You can write a fxml that attemts to do that without getting an error - that is until you attempt to load it. The java compiler would immediately complain about something like that.You can of course use java code in the controller class which allows you to combine fxml+java code to a certain level. E.g. repetetive structures could be created in the initialize()
method.
Scene builder is amazing when you are building project with JavaFX+FXML,i cant recommend it enough.
When you use fxml your classes become so much cleaner, and you can focus on further extending functionality of your containers/controls.You are still gonna be using javafx classes and your controllers might become quite big. Its good to know the concepts of how to structure layout and when to use what kind of container, so when you just started to learn basics, implement your layout in code, and then move to using SceneBuilder and FXML.
Once you implement more complex layouts in your code, you can translate these skills into other languages/platforms.
There are also cases where you cant do what you want via SceneBuilder, for instance some dynamic layout thats beign created at runtime and would be too tricky and messy to include into separated custom components/fxml files. Then you write it down in your code with complex logic on top of it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With