José
José

Reputation: 3179

JavaFX HBox hide item

How can i hide an item in HBox, and made space used by this item available to other items.

TitledPane legendPane = new TitledPane("Legend", _legend);
legendPane.setVisible(false);
LineChart chart = new LineChart<Number, Number>(_xAxis, _yAxis);

HBox hbox = new HBox(5);
hbox.getChildren().addAll(legendPane, chart);

In the above code i want the chart node to use all available space when the legend pane is hidden.

Upvotes: 30

Views: 21137

Answers (2)

jewelsea
jewelsea

Reputation: 159281

Before calling legendPane.setVisible, call:

legendPane.managedProperty().bind(legendPane.visibleProperty());

The Node.managed property prevents a node in a Scene from affecting the layout of other scene nodes.

Upvotes: 63

Uluk Biy
Uluk Biy

Reputation: 49185

You can temporarily remove it from the scene:

legendPane.visibleProperty().addListener(new ChangeListener<Boolean>() {
    @Override
    public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
        if (newValue) {
            hbox.getChildren().add(legendPane);
        } else {
            hbox.getChildren().remove(legendPane);
        }
    }
});

Or manipulate its size:

legendPane.visibleProperty().addListener(new ChangeListener<Boolean>() {
    @Override
    public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
        if (newValue) {
            legendPane.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
            legendPane.setPrefSize(prefWidth, prefHeight);
        } else {
            legendPane.setMaxSize(0, 0);
            legendPane.setMinSize(0, 0);

        }
    }
});

Upvotes: 7

Related Questions