I have a ToggleGroup of buttons which can be 3 buttons with the togglename choose an answers or 2 buttons with the togglename true or false. As a matter of fact the togglegroup contains buttons which correspond to asnwers (from questions to a quiz game I made in javafx). On the click event I want to locate which buttons was pressed and returned the correspondant value during the actionlistener.
My code is the following:`
groupAnswerQ = new ToggleGroup();
class MyChooseAnswerButton extends ToggleButton {
public MyChooseAnswerButton() {
setToggleGroup(groupAnswerQ);
setWrapText(true);
}
}
SanswerButton1 = new MyChooseAnswerButton();
SanswerButton2 = new MyChooseAnswerButton();
SanswerButton3 = new MyChooseAnswerButton();
groupAnswerQ.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
public void changed(ObservableValue<? extends Toggle> ov, Toggle toggle, Toggle new_toggle) {
if (new_toggle != null) {
textAnswerQ = (String) groupAnswerQ.getSelectedToggle().getUserData();
System.out.println("textAnswerQ : " + textAnswerQ);
}
}
});`
This is for the case of choose an answer (for the true or false is the similar code). My problem is that when the event is detected the textAnswerQ returns always null even when the new_toggle is not null. What am I doing wrong here?