Suemayah Eldursi
Suemayah Eldursi

Reputation: 1009

How to use controlsFX with FXML

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.

Upvotes: 1

Views: 1282

Answers (1)

fabian
fabian

Reputation: 82461

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
        }
    });

}

Upvotes: 4

Related Questions