2017-03-15 39 views
0

如果該行的內容被禁用且不可見,是否可以收縮GridPane行?JavaFX GridPane:如果內容被禁用且不可見,則收縮

當我將節點設置爲disable = true和visible = false時,單元仍佔用空間。

如果我有8行,只有第一個和最後一個可見,我不希望空行佔用太多的空間。就好像只有兩排。

我能找到的唯一「解決方案」是將大小設置爲零。但我不認爲這是一個很好的解決方案。如果節點再次啓用/可見,我將不得不存儲最小/最大大小以將其設置回。 也許CSS可以幫助更好的解決方案?

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

回答

2

如果你不想讓父母佈置一個節點,你可以在節點的managed屬性設置爲false

這裏是表示當我用在代碼中下面的行中的佈局的差圖像:

label2.setManaged(false); 

enter image description here

相關問題