I came across some documents on internet that in javafx you can apply controller to only parent element.
I have a javafx application (built with scenebuilder, eclipse) which has basic structure like this :
SplitPane
{
AnchorPane
{
}
AnchorPane
{
GridPane
{
Pane
{
Label
{
}
}
Pane
{
Label
{
}
}
}
}
}
I want to change the values of those labels at runtime. But it is throwing java.lang.NullPointerException
. My assumption why this is hapening is I'm applying controller to split pane and the labels are not direct children of it so I can't access them.
So questions are : 1) Is my assumption correct ? If not where I'm wrong or missing something ?
2) How to access the labels ?
3) Can I use controller for inner elements (not parent) ?
Thanks in advance, and sorry if the question doesn't make any sense, I'm very very new to javafx.
UPDATE : HERE'S THE CODE i'M USING AND EXCEPTION STACK TRACE :
public class Controller implements Initializable
{
@FXML
private SplitPane splitPane;
@FXML
private AnchorPane anchorPane1;
@FXML
private AnchorPane anchorPane2;
@FXML
private GridPane gridPane;
@FXML
private static Label z1;
@FXML
private static Label z2;
private HashMap<Integer,Label> zoneLabelNames = new HashMap<Integer,Label>();
public Controller()
{
// I have around 20 such labels which's value i'm setting using a loop. For now I've writter two only.
zoneLabelNames.put(0, z1);
zoneLabelNames.put(1, z2);
new Thread(() -> {
while(true)
{
try
{
//some logic to generate new values which will update labels
Platform.runLater(() -> {
//here I'm actually setting values in loop, for now I wrote only two labels.
for(int i =0;i<2;i++)
{
zoneLabelNames.get(i).setText("newly generated value"); // nullPointerException at this line
}
});
}
catch(Exception e)
{
e.printStackTrace();
}
}
}).start();
}