Question

Can anyone explain the difference between VBoxBuilder and VBox in JavaFX?

VBoxBuilder boxBuilder = VBoxBuilder.create();
VBox vBox1 = new VBox();
Was it helpful?

Solution 2

On builders and builder alternatives

Sergey's answer has this question covered, this is just some supplementary information.

There is a nice description of the builder functionality from one of the JavaFX builder creators in Advantages of JavaFX Builders.

However, as Sergey indicates, builders are deprecated from the core JavaFX platform. Oracle are busy removing all builder references from the JavaFX sample code.

Even though deprecated, builder functionality will be present and supported in JavaFX 8 (so for a long time to come).

Some alternatives to using the Java based JavaFX builders:

  • FXML can be used to provide a declarative syntax for development that is somewhat similar to builders.

  • JavaFX wrappers for other languages such as GroovyFX and ScalaFX provide builder style functionality as part of their core implementations by creating their own internal DSL for JavaFX object definition.

OTHER TIPS

Builders are added for convenience. They allow to create JavaFX nodes in one command without introducing new variable. It's more convenient in some situations.

Next two code snippets gives the same result, but latter doesn't create temp variable.

Without builder:

VBox vBox = new VBox();
vBox.setAlignment(Pos.CENTER);
vBox.getChildren().add(new Label("1"));
Scene scene = new Scene(vBox);

with builder:

Scene scene2 = new Scene( 
    VBoxBuilder.create().alignment(Pos.CENTER).children(new Label("1")).build());

N.B.: Although you may want to refrain from using builders as recently on open developers mail list an issue was raised which may lead to deprecating builders in the future releases: http://mail.openjdk.java.net/pipermail/openjfx-dev/2013-March/006725.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top