title: JavaFX TableView custom header
body: <p>i am customizing <code>JavaFX</code> <code>TableView</code>'s header.
  therefore i add a <code>Graphic</code> to the <code>Label</code>. By clicking the Label of the <code>header</code> i toggle my custom <code>header</code>(two lined). all this is working fine.</p>
  
  <p>The header gets automatically resized so the custom <code>header</code>fits in.
  BUT, when i hide my custom <code>header</code>the <code>header</code>stays large.</p>
  
  <p>What am i missing so the <code>header</code>shrinks again?</p>
  
  <p>i created a MCVE to demonstrate my problem:</p>
  
  <pre><code>public class TableViewHeaderMCVE extends Application {
  
      private final TableView&lt;Person&gt; table = new TableView&lt;&gt;();
  
      public static void main(String[] args) {
          launch(args);
      }
  
      @Override
      public void start(Stage stage) {
          final VBox root = new VBox();
          Scene scene = new Scene(root);
          stage.setWidth(218);
          stage.setHeight(216);
  
          TableColumn colName = new TableColumn("name");
          colName.setMinWidth(100);
          colName.setSortable(false);
  
          TableColumn colProfession = new TableColumn("profession");
          colProfession.setMinWidth(100);
          colProfession.setSortable(false);
  
          table.getColumns().addAll(colName, colProfession);
  
          root.getChildren().addAll(table);
  
          stage.setScene(scene);
          stage.show();
  
          // apply this after show!
          TableViewHeader.installMod(table);
      }
  
      public static class TableViewHeader {
          public static void installMod(TableView table) {
              for (Node n : table.lookupAll(".column-header &gt; .label")) {
                  if (n instanceof Label) {
                      new CustomHeaderLabel((Label) n);
                  }
              }
          }
      }
  
      public static class CustomHeaderLabel extends BorderPane {
  
          protected Label customNode = null;
          BooleanProperty expanded = new SimpleBooleanProperty(this, "expanded", false);
  
          public CustomHeaderLabel(final Label parent) {
              Label label = new Label(parent.getText());
  
              // custom MenuButton
              Button btn = new Button();
              btn.setGraphic(new Label("\u2261"));
              btn.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
              btn.setOnAction(new EventHandler&lt;ActionEvent&gt;() {
  
                  @Override
                  public void handle(ActionEvent ae) {
                      System.out.println("Hello World");
                  }
  
              });
  
              TextField filterTextField = new TextField();
              filterTextField.promptTextProperty().set("type here to filter");
  
              setCenter(label);
              setRight(btn);
              setBottom(filterTextField);
  
              EventHandler&lt;MouseEvent&gt; toggleHeader = new EventHandler&lt;MouseEvent&gt;() {
  
                  @Override
                  public void handle(MouseEvent me) {
                      expanded.set(!expanded.get());
                  }
  
              };
              parent.setOnMouseClicked(toggleHeader);
              expanded.addListener(new ChangeListener&lt;Boolean&gt;() {
  
                  @Override
                  public void changed(ObservableValue&lt;? extends Boolean&gt; obs, Boolean oldValue, Boolean value) {
                      showCustomHeader(value);
                  }
  
              });
  
              label.textProperty().bind(parent.textProperty());
              parent.setGraphic(this);
  
              customNode = parent;
              showCustomHeader(expanded.get());
          }
  
          protected void showCustomHeader(Boolean value) {
              if (value) {
                  customNode.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
              } else {
                  customNode.setContentDisplay(ContentDisplay.TEXT_ONLY);
              }
          }
      }
  
      public static class Person {
  
          private final SimpleStringProperty name;
          private final SimpleStringProperty profession;
  
          private Person(String name, String profession) {
              this.name = new SimpleStringProperty(name);
              this.profession = new SimpleStringProperty(profession);
          }
  
          public String getName() {
              return name.get();
          }
  
          public String getProfession() {
              return profession.get();
          }
      }
  }
  </code></pre>