Home > front end >  JavaFX table column won't resize to prefWidth
JavaFX table column won't resize to prefWidth

Time:12-16

I have a TableView in the center of a BorderPane with different prefWidth values for the columns. Inside SceneBuilder the columns are resized correctly according to the prefWidth value, but when I run the program the columns all have the same width (75.0). This is the .fxml file:

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="1500.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ReportController">
   <center>
      <TableView fx:id="reportTableView" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
        <columns>
          <TableColumn fx:id="date" prefWidth="60.0" text="Date" />
          <TableColumn fx:id="company" prefWidth="75.0" text="Company" />
          <TableColumn fx:id="number" prefWidth="40.0" text="Number" />
          ...
        </columns>
         <columnResizePolicy>
            <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
         </columnResizePolicy>
      </TableView>
   </center>
   <bottom>
      <TitledPane text="Summary" BorderPane.alignment="CENTER">
         <content>
            <HBox alignment="CENTER">
               <children>
                  <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Details:">
                     <font>
                        <Font size="20.0" />
                     </font>
                  </Text>
               </children>
            </HBox>
         </content>
      </TitledPane>
   </bottom>
</BorderPane>

And this is the code that loads the frame:

    try {
            FXMLLoader loader = new FXMLLoader(getClass().getClassLoader().getResource("report.fxml"));
            Parent parent = loader.load();

            Stage stage = new Stage(StageStyle.DECORATED);
            stage.setTitle("Tax Sheet Report");
            stage.getIcons().add(new Image("icons/icon.png"));
            stage.setScene(new Scene(parent));
            stage.setMaximized( true );
            stage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }

CodePudding user response:

Use UNCONSTRAINED_RESIZE_POLICY instead of CONSTRAINED_RESIZE_POLICY as a column policy of TableView.

CodePudding user response:

It is because you included the CONSTRAINED_RESIZE_POLICY to the table view.

The java doc says::

Simple policy that ensures the width of all visible leaf columns in this table sum up to equal the width of the table itself.

When the user resizes a column width with this policy, the table automatically adjusts the width of the right hand side columns. When the user increases a column width, the table decreases the width of the rightmost column until it reaches its minimum width. Then it decreases the width of the second rightmost column until it reaches minimum width and so on. When all right hand side columns reach minimum size, the user cannot increase the size of resized column any more.

Having said that, if you included the CONSTRAINED_RESIZE_POLICY on purpose, then you may need to add a bit more custom logic to satisfy the custom widths along with the policy.

  • Related