difference between VBoxBuilder and VBox in javafx - javafx-2

Can anyone explain the difference between VBoxBuilder and VBox in JavaFX?
VBoxBuilder boxBuilder = VBoxBuilder.create();
VBox vBox1 = new VBox();

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

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.

Related

How to achieve the following structure in javafx?

I have to create the architecture shown in the image below.
The inner components can be created very easily but in which container I would place them is the question. BorderPane, AnchorPane, GridPane.. These cannot contain JFXPanel as achild node.
Hence , the task is to convert the swing panel into jfxpanel. So each element should be of javafx.
JFXPanel is for embedding JavaFX scenes inside a Swing application, which doesn't sound like what your are doing, so a JFXPanel is probably the wrong component type to use for your application.
I guess (and I could be wrong) that your application is a JavaFX application in which you would like to embed a Swing component - in which case you use a SwingNode.
JavaFX scene
- JavaFX layout pane root
- JavaFX ScrollPane
- SwingNode
- Swing component
In general, mixing JavaFX and Swing is not usually recommended.
I note that your question is tagged javafx-2, so note that SwingNode only exists from Java 8 on; i.e., it does not exist in Java 7, so there is no way to accomplish the structure you outline in your question within the context of Java 7 or JavaFX 2.2.

jfxtras CalendarTextField cannot be loaded in Scene Builder

I am using Netbeans and Scenebuilder to create a JavaFX project. I realized that Scenebuilder does not have a date picker component so I downloaded the jfxtras and added the library to my classpath. I used the following code to add the CalendarTextField Object to my FXML file:
<?import jfxtras.labs.scene.control.*?>
<CalendarTextField fx:id="setupdate" prefWidth="200.0" showTime="true" GridPane.columnIndex="3" GridPane.rowIndex="8" />
It appeared to have worked ok, however, when I opened my FXML file in Scenebuilder I am getting the following error:
fxml:99: error:
javafx.fxml.LoadException: Element does not define a default property.
I am not sure what this means.
Java 8 DatePicker
Use Java 8 + SceneBuilder 2, it has a DatePicker.
Both are early access (especially SceneBuilder 2). Don't expect much in the way of SceneBuilder 2 stability for a little while, it's implementation is currently pretty rough and not as polished as SceneBuilder 1.1.
jfxtras and fxml
With regards to the use of jfxtras controls in SceneBuilder 1.1, my guess is that it is just not supported.
In order to build the jfxtras controls for Java 7, it was necessary for the jfxtras developers to make use of private APIs as feature complete public APIs for building custom controls are not available in Java 8. Usage of private APIs may cause compatibility issues with different SceneBuilder versions as it ties the control binaries to only work with certain JavaFX versions (as backward compatibility of private JavaFX apis is not guaranteed in new Java versions).
Also, the jfxtras developers may not have implemented the builder classes required to make their controls work with FXML. This would seem to be the case, as lack of an appropriate builder is what generates the error: javafx.fxml.LoadException: Element does not define a default property.
Implementing your own builder
You can read more about builders and fxml in an Introduction to FXML. You could implement a builder for the jfxtras CalendarTextField if you wish (I will not write one in this answer though). If you did implement an appropriate builder, there is a reasonable chance that you would then be able to use the control in FXML used by SceneBuilder 1.1.

extend class in Java FX?

In many occasions JavaFX needs to be customized with classes that extend existing ones. I tried this approach, for example to add a method to the NumberAxis class that would enable the label of the axis to be rotated.
But I got a "NumberAxis is declared final, can't be extended" compiler error. I wonder how people who extend classes do? Do they have access to the source code of javafx, modify it to make some classes not final, and recompile it? (sounds tricky! )
Making lots of classes final in the JavaFX framework was an intentional decision by the framework developers. To get a flavor of why it's done, see the Making Color Final proposal. That's just an example, there are other reasons. I think experience with subclassing in the Swing framework was that it caused errors and maintenance issues that the JavaFX designers wanted to avoid, so many things are made final.
There are other way to extend functionality than to directly subclass. Some alternatives for your rotation example:
aggregation: include the NumberAxis as a member of new class (e.g. NumberAxisWithRotatableText) which adds an accessor to get the underlying NumberAxis node and a method to perform the rotation (e.g. via a lookup as explained below).
composition: for example extend Pane, add a NumberAxis, disable the standard text drawing on the axis and add rotated labels yourself as needed.
css stylesheet: for example use a selector to lookup the text in the NumberAxis and the -fx-rotate attribute to rotate it.
node lookup: Use a node.lookup to get at the underlying text node, and apply the rotation via an API.
skin: All controls have a skin class attached them, replace the default skin class with a custom one.
subclass an alternate class: Subclass the abstract ValueAxis class rather than the final NumberAxis class.
Source code for JavaFX is available with build instructions. However, I don't recommend hacking a personal copy of the source code to remove final constructs unless you also submit it as an accepted patch to the JavaFX system so that you can be sure that your app won't break on a standard JavaFX install.
If you really think it is a good idea for a given class to be subclassable, then log a change request. Sometimes the JavaFX developers are overzealous and make stuff final which would be better not being final. NumberAxis perhaps falls into that category.

MvvmCross and Xcode Storyboard

The storyboard xcode are supported in mvvmcross v3?
If yes, how? There is an example?
Thanks
MvvmCross doesn't really fit that well with Storyboards
The reason is because Storyboards have some logic in them (eg Segue navigation) which really belongs inside actions in the MvvmCross ViewModels.
With that said... if you just want to take advantage of data-binding then you can... just:
add the MvvmCross assembly references
modify your Storyboard app so it runs some minimal setup code
use the Mvx*ViewController base classes instead of the UI*ViewController classes
and similarly use MvxTableViewCell (or similar) for the base class for any TableViewCell's you use.
There's no documentation around for this at the moment... but there is one sample - posted under the very odd title of 'eh' - https://github.com/slodge/eh - it's just a simple master-detail pair of views and it needs to be built against recent binaries - e.g from https://github.com/slodge/MvvmCross-Binaries/tree/master/XS-iOS-Mac
Not sure if this is really the SO way but Stuart and others have a better answer IMHO to this question when it was asked at a later date. See the answers to this question MVVMCross support for Xamarin.iOS Storyboards
I am answering here just to tie up the Q&A
UPDATE
MVVMCross 3.5.1 Now has a FromStoryboard attribute so that you can have some views which are Storyboard based. See http://slodge.blogspot.co.uk/2015/05/351-release.html
Thanks
Pat

What is the best way to design the GUI in javafx 2.0?

In javafx 2.0 it is possible to create the layout by using FXML approach or by using normal java code.
What is the best way with respect to a well designed set of UIs. In my application there is about 100 sub UIs.
Thanks
FXML looks more logical for that purpose. By using FXML
you split business logic from view
you get option to edit design without recompiling project.
you get design as structured xml tree which is much easier to edit comparing to potentially randomly ordered java code
with SceneBuider tool you get an option to use visual editor for your fxml files
Get JavaFX Scenebuilder here.
FXForm2 is a library providing automatic JavaFX 2.0 form generation.
however FXForm2 is not full WYSIWYG GUI design tool.
http://dooapp.github.io/FXForm2/
Scene builder should be a good starting point to create unique UIs of your application. Considering you have 100s of UIs, I assume that some of their "appearances" should be identical with slightly different functions. You can load the FXML dynamically and assign controller at run time. Which means 1 FXML file can be used with multiple controllers. Which can save you some time while keeping the code dynamic for easier maintenance.
FXMLLoader loader = new FXMLLoader(getClass().getResource("DBedit.fxml"));
loader.setController(new DBeditEntityUser());
So, to make use of the same FXML with a different controller.
FXMLLoader loader = new FXMLLoader(getClass().getResource("DBedit.fxml"));
loader.setController(new DBeditEntityUserLevel());
Hope this helps.
fyi,
road map for Java fx http://javafx.com/roadmap/ shows that the scene builder will be released around middle of the year. From the above web page:
"JavaFX Scene Builder is a WYSIWYG GUI design tool for the
JavaFX platform. It enables designing user interface screens by simply
dragging and positioning GUI components from a palette onto a scene.
The tool generates files in FXML format2 that can be used within a project
in any IDE such as NetBeans or Eclipse. The JavaFX Scene Builder can be
used to create GUI for desktop applications and applets that run in a browser."

Resources