JavaFX GridPane:如果内容被禁用并且不可见,则缩小 [英] JavaFX GridPane: Shrink if content is disabled and invisible
问题描述
如果该行的内容被禁用和不可见,是否可以缩小GridPane行?
Is it possible to shrink a GridPane row if the content of that row is both disabled and invisible?
当我将Node设置为disable = true和visible =
When I set a Node to disable=true and visible=false, the cell still takes up space.
如果我有8行,只有第一个和最后一个可见,我不想空行占用太多空间。好像只有两行。
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.
我能找到的唯一解决方案是将大小设置为零。但是,我认为这不是一个好的解决方案。当节点再次启用/可见时,我将必须存储最小/最大大小才能将其设置回去。
CSS可能会提供更好的解决方案吗?
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();
}
}
推荐答案
如果如果您不希望父节点布置节点,则可以将节点的托管
属性设置为 false
。
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);
这篇关于JavaFX GridPane:如果内容被禁用并且不可见,则缩小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!