1

I'm using the ControlsFX ToggleSwitch like so:

<ToggleSwitch fx:id="toggle" onAction="#handleToggleAction" mnemonicParsing="false" GridPane.columnIndex="1" />

I want to be able to associate actions on this ToggleSwitch with a method in my controller.

This is some of my code in the controller:

@FXML
private void handleToggleAction(ActionEvent event) throws IOException {
    Boolean selected = ((ToggleSwitch) event.getSource()).isSelected();

    if(selected) {
        //do something
    } else {
        //something else
    }
}

This is causing me an error:

Cannot determine type for property.

I don't know why this is causing an error. Before using a ToggleSwitch I was using a ToggleButton and the handler method was working fine. Any help appreciated.

fabian
  • 80,457
  • 12
  • 86
  • 114
Suemayah Eldursi
  • 969
  • 4
  • 14
  • 36

1 Answers1

4

ToggleSwitch simply does not contain a onAction property.

Therefore it's probably best to register a listener in the initialize method of the controller:

@FXML
private void initialize() {
    toggle.selectedProperty().addListener((observable, oldValue, newValue) -> {
        if(newValue) {
            //do something
        } else {
            //something else
        }
    });

}
fabian
  • 80,457
  • 12
  • 86
  • 114