how to fix javafx - tableview size to current window size

Rakesh SKadam picture Rakesh SKadam · Aug 12, 2016 · Viewed 9.5k times · Source

I'm totally new to standalone applications. Please any one help me on this.

I have TableView with 6 columns which is half showing in the window as shown below.

enter image description here

I want to fix its current window size, even when the window is expanded, the tableview should auto resize. Is there any way to do this?

This Is The Code Snippet

GridPane tableGrid= new GridPane();
tableGrid.setVgap(10);
tableGrid.setHgap(10);
Label schoolnameL= new Label(SCHOOL+school_id);
schoolnameL.setId("schoolLabel");
Button exportDataSheetBtn= new Button("Export In File");
tableView.setMaxWidth(Region.USE_PREF_SIZE);
tableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
tableGrid.getChildren().addAll(schoolnameL,exportDataSheetBtn,tableView);

Answer

Jonatan Stenbacka picture Jonatan Stenbacka · Aug 12, 2016

This can be done by binding the preferred height and width to the height and width of the primary stage. Here's an MCVE:

import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableView;
import javafx.stage.Stage;

public class MCVE extends Application {

    @Override
    public void start(Stage stage) {

        TableView<ObservableList<String>> table = new TableView<ObservableList<String>>();

        // We bind the prefHeight- and prefWidthProperty to the height and width of the stage.
        table.prefHeightProperty().bind(stage.heightProperty());
        table.prefWidthProperty().bind(stage.widthProperty());

        stage.setScene(new Scene(table, 400, 400));
        stage.show();
    }

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

}