2015-06-20 32 views
0

我有一個GridPane,我動態填充,然後顯示它,問題是:如果行數如此之多,那麼底部的行不顯示,我是什麼要的是增加一個滾動條,這樣我可以顯示所有的行,這裏是代碼,但它不工作: enter image description here enter image description hereGridPane的LayoutY不受ScrollBar監聽器的影響

BudgetManager bManager = new BudgetManager(); 
    List<Debt> debts = bManager.getDebts(); 
    GridPane topLevel = new GridPane(); 
    topLevel.setAlignment(Pos.TOP_CENTER); 
    int row = 0; 
    double totatB = 0, totalS = 0; 

    for (Debt d : debts) { 
     //fill GridPane 
    } 

    ScrollBar sc = new ScrollBar(); 
    sc.setMin(0); 
    sc.setMax(350); 
    sc.setVisibleAmount(20); 
    sc.setOrientation(Orientation.VERTICAL); 
    sc.setUnitIncrement(10); 
    sc.setBlockIncrement(50); 
    sc.setPrefWidth(20); 
    sc.valueProperty().addListener((ObservableValue<? extends Number> ov, Number old_val, Number new_val) -> { 
      topLevel.setLayoutY(-new_val.doubleValue()); 
     } 
    }); 
    HBox box = new HBox(); 
    box.getChildren().addAll(topLevel, sc); 
    HBox.setHgrow(topLevel, Priority.ALWAYS); 

    Scene scene = new Scene(box, 1200, 600); 
    stage.setScene(scene); 
    stage.show(); 

解決 這裏是解決方案,這要歸功於keuleJ,使用ScrollPane代替ScrollBar:

ScrollPane scroll = new ScrollPane(); 
    GridPane topLevel = new GridPane(); 

    scroll.setContent(topLevel); 
    scroll.setHbarPolicy(ScrollBarPolicy.ALWAYS); 
    scroll.setVbarPolicy(ScrollBarPolicy.AS_NEEDED); 
    scroll.setFitToWidth(true); 
    scroll.setFitToHeight(true); 

    Scene scene = new Scene(scroll, 1200, 600); // Manage scene size 
+0

你不應該使用一個ScrollPane是什麼? https://docs.oracle.com/javafx/2/api/javafx/scene/control/ScrollPane.html – keuleJ

+0

是的,我已經嘗試過,它的工作原理,但我想顯示在中心(而不是頂部左)的gridpane和它應該始終增長,如果用戶調整大小,scrollPane不會提供對齊屬性據我所知,這就是爲什麼我使用HBox – usertest

+0

fitToWidth或fitToHeight怎麼辦?您是否學習了文檔: ScrollPane允許應用程序設置用於在水平和垂直方向上定位內容的當前值,最小值和最大值。這些值按比例映射到包含節點的layoutBounds。 ScrollPane佈局計算基於layoutBounds而不是滾動節點的boundsInParent(可視邊界)。如果應用程序希望滾動基於節點的可視邊界(對於縮放內容等),則需要將滾動節點包裝到組中。 – keuleJ

回答