How do I make the last column in a JavaFX TableView take the remaining space.
I have tried table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY); but this makes the column sizes equal. I want only the last column to grow when the window width increases.
If you want all the columns in your tableview to fill up the window space available to the tableview AND be dynamically resized to adjust to changing the size of the window, then you need to bind the column properties to the tableview's width.
For example, say I have 5 columns in a tableview. I want them to always be a fixed percentage of the available width (so that when the window is resized, the proportions of the columns remain constant). You can easily form your own column sizing rules using the same property binding idiom (eg. I want the last column to take all the remaining space).
In the initialize() method of the controller that has my TableView control, I could do the following:
void initialize() {
// Initialize your logic here: all @FXML variables will have been injected
:
:
// TableView column control variables are prefixed with "tco"
tcoLast.setCellValueFactory(new PropertyValueFactory<Client.Expand, String>("lastName"));
tcoFirst.setCellValueFactory(new PropertyValueFactory<Client.Expand, String>("firstName"));
tcoDoB.setCellValueFactory(new PropertyValueFactory<Client.Expand, Integer>("doB"));
tcoMRN.setCellValueFactory(new PropertyValueFactory<Client.Expand, String>("defMRN"));
tcoGen.setCellValueFactory(new PropertyValueFactory<Client.Expand, String>("gender"));
// Cell factories for rendering certain columns in the TableView
tcoLast.setCellFactory(new ClientNameTableCellFactory());
tcoFirst.setCellFactory(new ClientNameTableCellFactory());
tcoDoB.setCellFactory(new ClientDoBTableCellFactory());
// Set fixed column widths that resize automatically
// Values are weighted to be a fraction of a total of 41 (arbitrary)
tcoLast.prefWidthProperty().bind(tbvMatches.widthProperty().multiply(11.0/41.0));
tcoFirst.prefWidthProperty().bind(tbvMatches.widthProperty().multiply(11.0/41.0));
tcoDoB.prefWidthProperty().bind(tbvMatches.widthProperty().multiply(8.0/41.0));
tcoMRN.prefWidthProperty().bind(tbvMatches.widthProperty().multiply(8.0/41.0));
tcoGen.prefWidthProperty().bind(tbvMatches.widthProperty().multiply(2.0/41.0));
:
:
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With