Mathomatic
Mathomatic

Reputation: 929

Adding a change listener to JavaFX ToggleSwitch

My stage contains a ToggleSwitch and two StackPanes which we'll call A and B. Both StackPanes are located in the same space within the parent StackPane. This means that if both A and B are visible and set to managed, they each take up half the allocated space like this:

enter image description here

I'm trying to hide StackPane B upon initalization so that StackPane A takes up the full space... and then when I click the toggle button, it should hide StackPane A and show StackPane B, making B take up the full space.

The initial hiding of StackPane B works fine, but I'm having trouble writing a change listener for the ToggleSwitch in my controller class. Here is my code, and where I'm having trouble:

application class:

public class showPanes extends Application {
Stage stage = new Stage();
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws IOException {
        StackPane root = (StackPane) FXMLLoader.load(Drag.class.getResource("twoPanes.fxml"));
        Scene scene = new Scene(root);
        stage.setTitle("Pane Switcher");
        scene.getStylesheets().add("styleMain.css");
        stage.setScene(scene);
        stage.show();
    }
}

The answer found here uses Toggle Groups, and James' answer here uses Buttons. I can't find a solution for ToggleSwitch. I tried to adapt the first answer for use with ToggleSwitch but it's producing an error like:

enter image description here

and says

Cannot resolve method 'addListener(anonymous javafx.beans.value.ChangeListener)'

How do I fix the listener?

controller class:

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.layout.StackPane;
import java.net.URL;
import java.util.ResourceBundle;
import org.controlsfx.control.ToggleSwitch;

public class compsController implements Initializable {
    @FXML
    private StackPane paneA, paneB;

    @FXML
    private ToggleSwitch toggleSwitch;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        paneB.setManaged(false);
        paneB.setVisible(false);

        toggleSwitch.selectedProperty().addListener(new ChangeListener < ToggleSwitch > () {
            @Override
            public void changed(ObservableValue < ? extends ToggleSwitch > ov, ToggleSwitch t, ToggleSwitch t1) {
                paneA.setManaged(false);
                paneA.setVisible(false);
                paneB.setManaged(true);
                paneB.setVisible(true);
            }
        });
    }
}

Upvotes: 1

Views: 3878

Answers (1)

Georgios Syngouroglou
Georgios Syngouroglou

Reputation: 19944

You could also use the Binding API of JavaFx like below,

@Override
public void initialize(URL location, ResourceBundle resources) {
    paneA.managedProperty().bind(toggleSwitch.selectedProperty());
    paneA.visibleProperty().bind(toggleSwitch.selectedProperty());
    paneB.managedProperty().bind(toggleSwitch.selectedProperty().not());
    paneB.visibleProperty().bind(toggleSwitch.selectedProperty().not());
}

}

Upvotes: 2

Related Questions