0

I am trying to get the width of a TableView object as wide as the accumulated sums of all TableColumns, so that the Table is fully shown without a scrollbar. The table is inside a grid Layout, and i tried getting the width of all columns together with the following code:

private double getTableWidth(TableView dataTable){
        double totalWidth = 0;
        for(int i = 0; i < dataTable.getColumns().size(); i++){
            totalWidth += ((TableColumn) dataTable.getColumns().get(i)).getWidth();
        }
        return totalWidth;
}

But somehow the width for each column is 80, even if they look different wide.

To set the width of the table i use the following:

dataTable.setMinWidth(getTableWidth(dataTable));

Note: I start with a TableView-Object with the following code:

children>
      <TableView fx:id="dataTable" prefHeight="451.0" prefWidth="701.0" />
   </children>

and fill the table by creating TableColumns without a width parameter or anything.

Picture of the current State: enter image description here

Mime
  • 1,142
  • 1
  • 9
  • 20

1 Answers1

4

You should set the column resize policy to constrained-resize, in this way you will fill all the width available divided for each column.

You can do it using FXML:

<TableView fx:id="tableView" layoutX="110.0" layoutY="78.0" prefHeight="200.0" prefWidth="396.0">
    <columnResizePolicy><TableView fx:constant="CONSTRAINED_RESIZE_POLICY"/></columnResizePolicy>
    <columns>
        <TableColumn prefWidth="75.0" text="Column X" />
        <TableColumn prefWidth="75.0" text="Column X" />
    </columns>
</TableView>

You can do it programmatically:

tableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);

You may want to look there

Snix
  • 541
  • 4
  • 12