Using JavaFX and Scene Builder, I am trying to access the text inside a Label from the controller.
I initialized a label as follows in the controller:
@FXML
private Label label1;
The following code is in the fxml file:
<Label fx:id="lable1" onMouseClicked="#handleLabel" text="Label 1" />
I have bound the label to this function in the controller using scene builder:
private void handleLabel(MouseEvent event) {
// get text from label n
}
My idea was to call something like label1.getText(). But for that I need to get the fx:id of the label, and since I will many labels, I cannot hardcode all the id cases individually.
Better way for Getting id of the clicked Object in JavaFX controller I have found this, but I do not think the this answer works for me because initializing the label and setting an id does not seem to cope with @FXML. Also, it has not been answered whether it is possible to access the fx:id of a component.
Any other way for how to access the Label object's methods like getText() are also welcome.
((Label)event.getSource()).getText()? (Even if you could get thefx:id, which you can't, how would you use that to callgetText()??) - James_Dlabel1(which is an object reference): you would have aStringvariable containing the text"label1", which is not the same thing at all. What would you do then? - James_Dfx:id="lable1"you should havefx:id="label1". Perhaps this typo or possible confusion with CSS id has lead you to create an XY problem. - jewelsea