2015-04-07 27 views
0

我想要一个在顶部有一个可调整大小的图表和在底部有一个固定大小的表格的布局。我尝试使用VBoxChartTableView,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; 
} 

有没有办法调整表格的内容并修复它?

我已经在桌子上尝试setMaxHeightsetPrefHeightUSE_PREF_SIZEUSE_COMPUTED_SIZE没有效果

+0

您可以尝试使用minHeight和maxHeight的组合。例如,'table.setMaxHeight(120); table.setMinHeight(120);'。 – ItachiUchiha

+0

这没有奏效。打印table.getHeight()表明高度*被约束 - 但布局似乎忽略了这一点。 –

回答

0

表首先显示更多行比我

这是TableView默认样式。你可以通过自定义表格单元格的css来隐藏它们。但是随后出现了另一个问题,tableview仍然会显示没有行的空白区域。但是,如果数量或项目(行)是固定的,则这两个问题都可以解决。这可以通过简单地设置的tableview的优选宽度和高度来完成:

table.setPrefSize(300, 100); 

其次,当我调整主窗口

这是VBox默认行为也调整大小。这里把tableview放到另一个Parent不能直接调整大小,将解决调整大小的问题。大多数时候,优秀的候选人是Group

Group group = new Group(table); 

其他视觉上铺设的提示是:

  • 对齐垂直框儿童中心: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; 
} 
+0

它似乎也适用于'table.setPrefHeight(100)'并且只保留宽度。 –