我有一个动态填充的GridPane,然后显示它,问题是:如果行太多,那么底部的行不会显示,我想要的是添加一个滚动条,以便我可以显示所有行,这是代码,但它不起作用:
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
最佳答案
使用 fitToWidth 或 fitToHeight 属性。
查看文档: https://docs.oracle.com/javafx/2/api/javafx/scene/control/ScrollPane.html
关于java - GridPane 的 LayoutY 不受 ScrollBar Listener 影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30953672/