java JavaFX HBox 隐藏项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12200195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:01:20  来源:igfitidea点击:

JavaFX HBox hide item

javajavafx-2

提问by José

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

我如何在 HBox 中隐藏一个项目,并使该项目使用的空间可用于其他项目。

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.

在上面的代码中,我希望图表节点在图例窗格隐藏时使用所有可用空间。

回答by jewelsea

Before calling legendPane.setVisible, call:

在调用legendPane.setVisible 之前,调用:

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

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

所述Node.managed特性防止一个节点在一个场景影响其他场景节点的布局。

回答by Uluk Biy

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

        }
    }
});