u4547878
u4547878

Reputation: 197

javafx tableview column resizing issue

I am using Sceenbuilder to design the GUI. In the TableView i have 9 columns. All columns except one same width is fine. Here is the screenshot from sceenbuilder: enter image description here

I only want the column "serial" smaller in width. So i set resize policy to CONSTRAINED_RESIZE_POLICY. And disable only resizing only for "serial" column and set it to a fixed size(such as 50).

In sceenbuilder preview it is working fine but when i compile the code i get a empty column at the end like this: enter image description here

Only after resizing any column using mouse it gets desired width automatically. My question is Why should it first require to drag any column using the mouse? This is what i don't want!.

How can i solve it?

Upvotes: 3

Views: 3815

Answers (1)

Hypnic Jerk
Hypnic Jerk

Reputation: 1192

I had the same issue on my program, and I fixed it this way.

Leave the TableView as CONSTRAINED_RESIZE_POLICY. Then, on the TableColumn you want a certain width, set the max , min, and pref size to the same size. This way, it will be no bigger and no smaller than your size.

Edit 1:

After playing around with it, I've determined you only NEED to set the max width of your TableColumn to whatever size you want it to be, as this will allocate the appropriate space on the table header, my example below still works though.

Example below:

Environment.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.BorderPane?>


<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.91" xmlns:fx="http://javafx.com/fxml/1">
   <center>
      <TableView prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
        <columns>
          <TableColumn prefWidth="75.0" text="First Name" />
          <TableColumn prefWidth="75.0" text="Last Name" />
            <TableColumn prefWidth="75.0" text="Email Address" />
            <TableColumn maxWidth="50.0" minWidth="50.0" prefWidth="50.0" text="Age" />
            <TableColumn maxWidth="50.0" minWidth="50.0" prefWidth="50.0" text="Height" />
        </columns>
         <columnResizePolicy>
            <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
         </columnResizePolicy>
      </TableView>
   </center>
</BorderPane>

Main. In my example, the fxml file is in a directory named fxml. Please adjust code for this.

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;

public class Main extends Application {


    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws IOException {

        FXMLLoader loader = new FXMLLoader(getClass().getClassLoader().getResource("fxml/Environment.fxml"));
        Parent root = loader.load();
        Scene scene = new Scene(root,800,800);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

Upvotes: 2

Related Questions