VBox 中 TableView 的大小
Size of TableView in a VBox
我想要一个顶部有可调整大小图表和底部固定大小 table 的布局。我尝试将 VBox
与 Chart
和 TableView
以及 VBox.setVgrow
一起使用,以确保所有调整大小都在图表上进行。我事先知道 table.
中只有 3 行
但是,table 首先显示的行数比我多,其次,当我调整主 window.
的大小时也会调整大小
我在 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();
}
并且 table 是这样初始化的(注意:所有带有 Color
的东西只是为了将一些数据放入 table)
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;
}
有没有办法根据其内容调整 table 的大小并修复它?
我已经在 table 上尝试 setMaxHeight
和 setPrefHeight
与 USE_PREF_SIZE
和 USE_COMPUTED_SIZE
没有效果
the table firstly shows more rows than I have
这是TableView
的默认样式。您可以通过自定义 table 单元格的 css 来隐藏它们。但是另一个问题出现了,table视图仍然会显示没有行的空白区域。然而,如果数量或项目(行)是固定的,这两个问题都可以解决。这可以通过简单地设置 tableview:
的首选宽度和高度来实现
table.setPrefSize( 300, 100 );
and secondly, also resizes when I resize the main window
这是 VBox
的默认行为。这里将 table 视图放到另一个不能直接调整大小的 Parent
视图将解决调整大小问题。大多数时候,好的候选人是 Group
.
Group group = new Group( table );
其他视觉布局提示是:
- 居中对齐 VBox 子项:
root.setAlignment( Pos.CENTER );
- 给 Vbox 一些填充:
root.setPadding( new Insets( 15 ) );
对您的代码进行全面修改:
@Override
public void start( Stage primaryStage )
{
LineChart<Number, Number> chart = new LineChart<>( new NumberAxis(), new NumberAxis() );
Node table = makeTable();
Group group = new Group( table );
VBox.setVgrow( chart, Priority.ALWAYS );
VBox.setVgrow( group, Priority.NEVER );
VBox root = new VBox( chart, group );
root.setAlignment( Pos.CENTER );
root.setPadding( new Insets( 15 ) );
Scene scene = new Scene( root, 300, 250 );
primaryStage.setScene( scene );
primaryStage.show();
}
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.setPrefSize( 300, 100 );
table.getColumns().setAll( Arrays.asList( col1, col2, col3 ) );
return table;
}
我想要一个顶部有可调整大小图表和底部固定大小 table 的布局。我尝试将 VBox
与 Chart
和 TableView
以及 VBox.setVgrow
一起使用,以确保所有调整大小都在图表上进行。我事先知道 table.
但是,table 首先显示的行数比我多,其次,当我调整主 window.
的大小时也会调整大小我在 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();
}
并且 table 是这样初始化的(注意:所有带有 Color
的东西只是为了将一些数据放入 table)
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;
}
有没有办法根据其内容调整 table 的大小并修复它?
我已经在 table 上尝试 setMaxHeight
和 setPrefHeight
与 USE_PREF_SIZE
和 USE_COMPUTED_SIZE
没有效果
the table firstly shows more rows than I have
这是TableView
的默认样式。您可以通过自定义 table 单元格的 css 来隐藏它们。但是另一个问题出现了,table视图仍然会显示没有行的空白区域。然而,如果数量或项目(行)是固定的,这两个问题都可以解决。这可以通过简单地设置 tableview:
table.setPrefSize( 300, 100 );
and secondly, also resizes when I resize the main window
这是 VBox
的默认行为。这里将 table 视图放到另一个不能直接调整大小的 Parent
视图将解决调整大小问题。大多数时候,好的候选人是 Group
.
Group group = new Group( table );
其他视觉布局提示是:
- 居中对齐 VBox 子项:
root.setAlignment( Pos.CENTER );
- 给 Vbox 一些填充:
root.setPadding( new Insets( 15 ) );
对您的代码进行全面修改:
@Override
public void start( Stage primaryStage )
{
LineChart<Number, Number> chart = new LineChart<>( new NumberAxis(), new NumberAxis() );
Node table = makeTable();
Group group = new Group( table );
VBox.setVgrow( chart, Priority.ALWAYS );
VBox.setVgrow( group, Priority.NEVER );
VBox root = new VBox( chart, group );
root.setAlignment( Pos.CENTER );
root.setPadding( new Insets( 15 ) );
Scene scene = new Scene( root, 300, 250 );
primaryStage.setScene( scene );
primaryStage.show();
}
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.setPrefSize( 300, 100 );
table.getColumns().setAll( Arrays.asList( col1, col2, col3 ) );
return table;
}