Can't set FXML label from controller - javafx

I can't work out how to set the value of a Label defined in FXML from the controller initializer.
In my FXML I have (in a HBox):
<Label text="Label: " />
<Label fx:id="myLabel" />
And in the controller I have:
#FXML
private Label myLabel;
#FXML
private void initialize() {
myLabel.setText("Hello!");
}
The label stays blank when I run the program. What am I doing wrong? (I suspect initialize() is not running - the Eclipse debugger doesn't stop in it when I set a breakpoint - but I'm not sure and I can't get JavaFX to respect console printf statements for me to do debug print statements.)

Related

Griffon FXML button text value is overwritten

Griffon 2.10.0
FXML:
<Button layoutX="172.0" layoutY="45.0" JavaFXUtils.griffonActionId="click2"
mnemonicParsing="false" text="Test"
prefWidth="200.0"
/>
TestView.groovy:
void initUI() {
builder.application(title: application.configuration['application.title'], name: "settingsWindow",
centerOnScreen: true, resizable: false) {
scene {
def node = loadFromFXML("views/test.fxml")
connectActions(node, controller)
inputx.textProperty().bindBidirectional(model.inputProperty())
fxml node
}
}
}
Shows this button with the text "Click2"
Why is the text="Test"configuration in the FXML overwritten by the griffonActionId name?
It works when I use this in FXML:
<Button fx:id="btn1" ...
And in TestView.groovy:
#FXML private Button btn1
...
btn1.text = 'Test'
This displays:
Is this the proper the way to set the button text?
This is expected behavior. Buttons configured via Griffon actions will bind their properties to action properties. The button's text is bound to the action's name by default. You can overrode this behavior by setting the button's text explicitly in code, like you did.

see an imageview created in Fxml at the java class by id

I have a problem in JavaFX with FXML , i created an ImageView in FXML and gave it an id . how can i simply use the ImageView that i have created in FXML in the java class code ?
When defining the ImageView in your controller, you need to add the #FXML annotation before it.
An example in Java would be like so:
#FXML
private Canvas canvas;
And in the FXML file:
<center>
<Canvas fx:id="canvas" height="600" width="600" />
</center>
Just keep in mind that the name in Java must be the same as the id in the FXML file

Javafx FXML loader getcontroller returns null

In my program i have a tabPane with each tab having in own FXML file and controller .I have loaded the FXML file for tab 2 (Schedular).I`m trying to call a function in my child controller (Scheduler) from my parent controller(FXML Document) . When i load the controller using FXML Loader it returns a null. How can i solve this .
Here is my main code:
Main FXMLDocument :
<AnchorPane xmlns:fx="http://javafx.com/fxml/1" fx:id="AnchorPane" prefHeight="600" prefWidth="800" xmlns="http://javafx.com/javafx/8.0.65" fx:controller="showprojavafxml.FXMLDocumentController">
<children>
The FXMLLoader only instantiates the controller when it loads the fxml file (this has to be the case, since the controller class is specified in the fxml file...). Since you never call load() on the loader, the controller is never created.
It looks like you are referencing the FXML file twice through two different mechanisms: once in FXMLDocumentController.initialize(), where you create a FXMLLoader whose location is set to the fxml file, and once in the main FXML document itself, via a <fx:include>. The <fx:include> is causing the UI defined in Scheduler.fxml to be displayed; the FXMLLoader you create in the initialize() method is not (because you never call load() and display the result).
To reference a controller for an included fxml, use the "Nested controller" technique.
First, add an fx:id to your fx:include:
<Tab fx:id="tab2" text="Scheduler" >
<fx:include fx:id="scheduler" source="Scheduler.fxml" />
</Tab>
Now you can inject the controller into a field whose name is the fx:id with the text "Controller" appended:
public class FXMLDocumentController {
#FXML
private SchedulerController schedulerController ;
#Override
public void initialize(URL url, ResourceBundle rb) {
schedulerController.refreshList();
}
}

Random NullPointerExceptions in JavaFX

My first question on SO.
So I am just starting to learn JavaFX, and I already have taken a year of normal Java coding at school, so I was a bit perplexed when I came across this problem:
I started creating a basic GUI in JavaFX Scene Builder which has a few buttons, a textField, and a statusIndicator, and when I click buttonListen I want buttonToggle to disappear and have the status indicator (spinWheel) appear. Here is the code for handling that:
#FXML
private TextField fieldDisplay;
private Button buttonToggle;
private Button buttonListen;
private ProgressIndicator spinWheel;
#FXML
private void ButtonListenListener(ActionEvent event){
if(buttonToggle.isVisible()){
buttonToggle.setVisible(false);
spinWheel.setVisible(true);
}
}
All of that is contained within an FXMLDocumentController.java class for those familiar with JavaFX. However, when I run the program, there is a big long error stack, but it boils down to this statement saying that there is a nullPointer on one of my Buttons:
Caused by: java.lang.NullPointerException
at javafxfirstproj.FXMLDocumentController.ButtonListenListener(FXMLDocumentController.java:45)
I've heard that JavaFX is riddled with bugs, so I'm wondering if this is a logic error in my code or if I've just run into one of the (supposedly) many bugs. Any and all help would be appreciated. Thanks.
I'd need to see more of your code (specifically, the FXML and the code that loads it) to be sure, but I'm guessing the problem is here:
#FXML
private TextField fieldDisplay;
private Button buttonToggle;
private Button buttonListen;
private ProgressIndicator spinWheel;
That #FXML notation only applies to the declaration immediately following it (the declaration of fieldDisplay). If buttonToggle, buttonListen, and spinWheel are also linked to FXML, you need to annotate each of them as well:
#FXML
private TextField fieldDisplay;
#FXML
private Button buttonToggle;
#FXML
private Button buttonListen;
#FXML
private ProgressIndicator spinWheel;
If you are creating/declaring of Javafx components/nodes/control in FXML and you want to use it in the Java controller class then with each and every component that you want to use should have #FXML annotation with them. So in you code add FXML annotations
#FXML
private TextField fieldDisplay;
#FXML
private Button buttonToggle;
#FXML
private Button buttonListen;
#FXML
private ProgressIndicator spinWheel;
If you don't then these components are not assigned any objects thus when you use it like this
if(buttonToggle.isVisible())
it will give you NullPointerException
Checkout this blog's Connection to code section

How to add tags to an fxml file through controller in javafx?

<AnchorPane>
<TreeView fx:id="locationTreeView" focusTraversable="true" prefHeight="449.0" prefWidth="725.0" style="#tree
{
-fx-border-style:solid;
-fx-border-width:1px;
-fx-border-color:#ffffff;
}"/>
In the above fxml code I want to add one more <TreeView> but through the controller. How can I do this?
You will have to:
Give a fx:id to the AnchorPane:
<AnchorPane fx:id="theAnchorPane">
Add the corresponding field in the controller:
#FXML private AnchorPane theAnchorPane;
From the code that performs the addition you have to:
Create the new TreeView however you like:
TreeView newTreeView = ...;
Add it to the childen of the AnchorPane, possibly with some constraints:
theAnchorPane.getChildren().add(newTreeView);
AnchorPane.setTopAnchor(newTreeView, ...); // etc

Resources