title: Adding a change listener to JavaFX ToggleSwitch
body: <p>My stage contains a <code>ToggleSwitch</code> and two <code>StackPanes</code> which we'll call A and B. Both StackPanes are located in the same space within the parent <code>StackPane</code>. This means that if both A and B are visible and set to managed, they each take up half the allocated space like this:</p>
  
  <p><a href="https://i.stack.imgur.com/R7EYi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/R7EYi.png" alt="enter image description here"></a> </p>
  
  <p>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.</p>
  
  <p>The initial hiding of StackPane B works fine, but I'm having trouble writing a change listener for the <code>ToggleSwitch</code> in my controller class. Here is my code, and where I'm having trouble:</p>
  
  <p><strong>application class:</strong></p>
  
  <pre><code>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();
      }
  }
  </code></pre>
  
  <p>The answer found <a href="https://stackoverflow.com/a/21374681/4594511">here</a> uses Toggle Groups, and James' answer <a href="https://stackoverflow.com/a/26693941/4594511">here</a> uses Buttons. I can't find a solution for <code>ToggleSwitch</code>. I tried to adapt the first answer for use with <code>ToggleSwitch</code> but it's producing an error like: </p>
  
  <p><a href="https://i.stack.imgur.com/vBvhE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vBvhE.png" alt="enter image description here"></a></p>
  
  <p>and says </p>
  
  <blockquote>
    <p>Cannot resolve method 'addListener(anonymous
    javafx.beans.value.ChangeListener)'</p>
  </blockquote>
  
  <p>How do I fix the listener?</p>
  
  <p><strong>controller class:</strong></p>
  
  <pre><code>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 &lt; ToggleSwitch &gt; () {
              @Override
              public void changed(ObservableValue &lt; ? extends ToggleSwitch &gt; ov, ToggleSwitch t, ToggleSwitch t1) {
                  paneA.setManaged(false);
                  paneA.setVisible(false);
                  paneB.setManaged(true);
                  paneB.setVisible(true);
              }
          });
      }
  }
  </code></pre>