create javafx reusable components - javafx

When I was creating some app using javafx, I found similar constructs appear multiple times in my fxml files:
<HBox alignment="CENTER_LEFT" spacing="5">
<padding>
<Insets bottom="5.4" left="6.5" right="6.5" top="5.4" />
</padding>
<children>
......
</children>
</HBox>
The only differences are the children. I want to have some way to avoid specifying the common attributes everywhere. Is there a easy way to do that?
I know we can have custom components in javafx but that requires implementing a custom Controller class, which is over kill for my purpose.

Create a separate fxml for commonly used components and then add them in another fxml using fx:include tag..

Related

Add red asterisk to the beginning of text to a javafx label

I have a scene (created in SceneBuilder) with a number of labels some of which I want to show as required fields by adding a red asterisk at the beginning of the label text. I cannot see how to do this in SceneBuilder. Is there a way of doing this using CSS.
Below is the label snippet form the FXML file:
<Label fx:id="lblFirstName" alignment="CENTER_RIGHT" prefHeight="17.0" prefWidth="295.0" text="First Name:" GridPane.columnIndex="1" GridPane.rowIndex="2">
<padding>
<Insets right="10.0" />
</padding>
</Label>
Everything I have found so far relates to HTML files.
Slaw suggestion is good, but I would suggest another way using validation.
Note that this method requires ControlsFX library and some code in your controller (can't be done in css as you mentioned).
In your controller class create a validator:
ValidationSupport validationSupport = new ValidationSupport();
Add validation (as a required field) to your node (TextField in this example) inside initialize method:
validationSupport.registerValidator(myTextField, Validator.createEmptyValidator("Field is required"));
Now when you run your application the required TextField will look like this:
If the focus is lost from TextField and it doesn't have any text in it, it will show a red X like this:

Creating an empty VBox in FXML and later adding nodes in javafx doesn't work

In my FXML i have a simple empty VBox inside an AnchorPane:
<VBox fx:id="clients" spacing="10" AnchorPane.rightAnchor="50.0" AnchorPane.topAnchor="150.0">
<children>
</children>
</VBox>
Then I want to add a node to the VBox in java class:
clients.getChildren().add(0, customObject.getGridPane());
But the node doesn't show up, VBox keeps empty.
If I simply add any node to the FXML then later adding a node in java works but not if it starts empty.
This works:
<VBox fx:id="clients" spacing="10" AnchorPane.rightAnchor="50.0" AnchorPane.topAnchor="150.0">
<children>
<Label />
</children>
</VBox>
It must be an easy thing I'm missing but couldn't find the answer anywhere.
I tried it and it's working fine the problem is in this code customObject.getGridPane() make sure that it get a node I added a Button and it's working fine
Note: label didn't show because of text color try a button
use addAll method instead of add
clients.getChildren().addAll(0, customObject.getGridPane());

Gluon Scene Builder 8.3.0 - FXML fails to load when contain fx:include

Part of my FXML file contains the code at the end of this thread. It seems that the new Scene Builder (8.3.0) does not support the < fx:include/> part, cause when I try to open the FXML file it returns the following exception.
Caused by: javafx.fxml.LoadException:
/D:/workspace/javafx/src/SearchScreen.fxml:159
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2425)
at com.oracle.javafx.scenebuilder.kit.fxom.FXOMLoader.load(FXOMLoader.java:89)
... 50 more
Caused by: java.lang.ClassCastException: com.oracle.javafx.scenebuilder.kit.fxom.TransientIntrinsic cannot be cast to com.oracle.javafx.scenebuilder.kit.fxom.TransientObject
at com.oracle.javafx.scenebuilder.kit.fxom.FXOMLoader.endElement(FXOMLoader.java:315)
at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2826)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532)
... 52 more
When I comment out the < fx:include/> part, then the FXML opens just fine in the Scene Builder. I remember that one of the previous versions of Scene Builder did not have this problem. Is there any link with the previous versions available for download?
<fx:include fx:id="searchPane" source="newSearch/SearchPane.fxml" style="-fx-border-color: white; -fx-background-color: white; -fx-background: white;" GridPane.columnIndex="1" GridPane.rowIndex="8">
<GridPane.margin>
<Insets right="10.0" />
</GridPane.margin>
</fx:include>
Thanks in advance
Update
I downloaded all the previous versions from the link below, and none fix it. So, I am not sure now which version I had before that was working OK with.
https://bitbucket.org/gluon-oss/scenebuilder/downloads/?tab=tags

Switch to another file page in JavaFX

I'm making a simple application for school, but one thing just wont work. I'm trying to make a button, that when you press it, you go to another page, aka, loads a new fxml file.
here's my code for the buttons so far.
<Button prefWidth="240.0" styleClass="first, active" text="HOME">
<VBox.margin>
<Insets top="40.0" />
</VBox.margin>
</Button>
<Button prefWidth="240.0" styleClass="first, active" text="Dashboard">
<VBox.margin>
<Insets top="40.0" />
</VBox.margin>
</Button>
and I cant seem to make the button for Dashboard work so that it loads the Dashboard Page
i'm not sure if I totally understood your question but here's a possible solution : Follow the tutorial in this : managing multiple screens in javafx
it's clear, easy and well organised and you can dowload the full implementation with three testing screens

Element does not define a default property

I'm working with JavaFX and trying to use FXML, however I've never had any sort of formal training in it, so I'm kind of stumbling about.
I keep running into this here error:
Caused by: javafx.fxml.LoadException: Element does not define a default property.
My aim is to try to initialize a Custom Controller class that runs its own FXML file. The code example given by Oracle therefore looks like this:
<fx:root type="javafx.scene.layout.VBox" xmlns:fx="http://javafx.com/fxml">
<TextField fx:id="textField"/>
<Button text="Click Me" onAction="#doSomething"/>
</fx:root>
where the controller and root is set in the Controller method.
I'm trying to adapt this code to my own needs, and I was wondering if someone could explain to me why this error comes up if the fx:root type="javafx.scene.layout.VBox" is ever changed to something like fx:root type="javafx.scene.Parent" if you want me to, I can post some actual code samples.
The concept of default-property is introduced to short circuit your FXML in reality you'd have to write:
<fx:root type="javafx.scene.layout.VBox" xmlns:fx="http://javafx.com/fxml">
<children>
<TextField fx:id="textField"/>
<Button text="Click Me" onAction="#doSomething"/>
</children>
</fx:root>
if you now change the container type to Parent and you browse the parent class you'll notice that it does not define a children-property which you implicitly assume - children is introduced by Pane.
You should probably read http://docs.oracle.com/javafx/2/api/javafx/fxml/doc-files/introduction_to_fxml.html

Resources