2015-04-07 25 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)'並且只保留寬度。 –