我想要一个在顶部有一个可调整大小的图表和在底部有一个固定大小的表格的布局。我尝试使用VBox
与Chart
和TableView
,VBox.setVgrow
确保所有调整大小都在图表上。我事先知道我只会在桌上有三排。VBox中TableView的大小
但是,表格首先显示比我有更多的行,其次,当调整主窗口大小时也会调整大小。
我在Windows 8.1/64位上使用JDK8更新31。
我设置了我的舞台这样
public void start(Stage primaryStage) {
LineChart<Number, Number> chart = new LineChart<>(new NumberAxis(), new NumberAxis());
Node table = makeTable();
VBox.setVgrow(chart, Priority.ALWAYS);
VBox.setVgrow(table, Priority.NEVER);
VBox root = new VBox(chart, table);
Scene scene = new Scene(root, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
,并表被初始化像这样的(注:所有Color
的东西,只是有一些数据放在表)
private static Node makeTable() {
TableView<Color> table = new TableView<>(FXCollections.observableArrayList(Color.ALICEBLUE, Color.CHARTREUSE, Color.GOLDENROD));
TableColumn<Color, Double> col1 = new TableColumn<>("Red");
TableColumn<Color, Double> col2 = new TableColumn<>("Green");
TableColumn<Color, Double> col3 = new TableColumn<>("Blue");
col1.setCellValueFactory(new PropertyValueFactory<>("red"));
col2.setCellValueFactory(new PropertyValueFactory<>("green"));
col3.setCellValueFactory(new PropertyValueFactory<>("blue"));
table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
table.getColumns().setAll(Arrays.asList(col1, col2, col3));
return table;
}
有没有办法调整表格的内容并修复它?
我已经在桌子上尝试setMaxHeight
和setPrefHeight
与USE_PREF_SIZE
和USE_COMPUTED_SIZE
没有效果
您可以尝试使用minHeight和maxHeight的组合。例如,'table.setMaxHeight(120); table.setMinHeight(120);'。 – ItachiUchiha
这没有奏效。打印table.getHeight()表明高度*被约束 - 但布局似乎忽略了这一点。 –