Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use SceneBuilder or implement layout in code?

Tags:

java

javafx

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?

like image 421
BigRedChick Avatar asked Jan 07 '18 10:01

BigRedChick


2 Answers

Use the right tool for the right job

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

Pros of FXML

  • Fast to design
  • WYSYWYG Editor(SceneBuilder)
  • Scenes can be modified without the need to recompile the app
  • Nice seperation of view and controller with injection
  • In most cases fxml structure resembles the object structure closely making it easy to read.

Cons (compared to java code)

  • loading a fxml is slower than java code
  • no loops possible. When you need to insert something 10 times into a scene you need at least 10 tags; you have to insert the thing 100 times? Thats at least 100 tags. A single tag may not even suffice, e.g. for a GridPane there is no way to insert multiple nodes to multiple columns per <fx:include> tag.
  • Storing nodes into a collection requires assiging different fx:ids to all the elements and then listing all of them at a different location.
  • No compile-time checks. You want to add a 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.
  • There is just no way to invoke some methods from fxml.
  • Loading fxmls relies on reflection

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.

like image 97
fabian Avatar answered Sep 24 '22 15:09

fabian


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.

like image 35
Tomas Bisciak Avatar answered Sep 24 '22 15:09

Tomas Bisciak