JavaFX GridPane: Shrink if content is disabled and invisible

落花浮王杯 提交于 2020-07-04 03:28:12

问题


Is it possible to shrink a GridPane row if the content of that row is both disabled and invisible?

When I set a Node to disable=true and visible=false, the cell still takes up space.

If I have 8 rows and only the first and last is visible I don't want the empty rows to take up much space. As if there was only two rows.

The only "solution" I could find was to set the size to zero. However I do not consider that a good solution. I would have to store the min/max size to set it back when/if the node becomes enabled/visible again. Could perhaps CSS help with a better solution?

package com.company;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;

public class App extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /* (non-Javadoc)
     * @see javafx.application.Application#start(javafx.stage.Stage)
     */
    @Override
    public void start(Stage stage) throws Exception {
        Label label1 = new Label("Text");
        Label label2 = new Label("Text");
        Label label3 = new Label("Text");

        label2.setDisable(true);
        label2.setVisible(false);

        GridPane root = new GridPane();
        root.setGridLinesVisible(true);
        root.add(label1, 0, 0);
        GridPane.setVgrow(label1, Priority.NEVER);
        root.add(label2, 0, 1);
        GridPane.setVgrow(label2, Priority.NEVER);
        root.add(label3, 0, 2);
        GridPane.setVgrow(label3, Priority.NEVER);

        stage.setScene(new Scene(root));
        stage.setWidth(300);
        stage.setHeight(300);
        stage.setTitle("JavaFX 8 app");
        stage.show();
    }
}

回答1:


If you do not want the parent to layout a node, you can set the managed property of the node to false.

Here is an image showing the difference in layout when I used the following line in your code:

label2.setManaged(false);



来源:https://stackoverflow.com/questions/42802823/javafx-gridpane-shrink-if-content-is-disabled-and-invisible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!