how to fix javafx - tableview size to current window size
Asked Answered
B

4

8

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);
Beamon answered 12/8, 2016 at 7:15 Comment(1)
Adding some code related to TableView and its parent container would help us help you better.Selfappointed
S
12

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();
    }

}
Sphingosine answered 12/8, 2016 at 8:2 Comment(0)
E
5

Create Tabel inside GridPane and Use GridePane inside AnchorPane.Click on GridPane(capture1). enter image description here Click on Ancho Pane Constraint inside the GridPane Layout (captur2). enter image description here

In table view Layout select Hgrow and Vgrow as 'Always'(capture3)enter image description here

Ehrlich answered 20/7, 2018 at 11:12 Comment(2)
This worked for me - all I needed to do was to set Hgrow and Vgrow, thanksHern
works on ArchonePane as wellFishhook
F
4
VBox.setVgrow(tableView, Priority.ALWAYS);

OR for your parent layouts:

 VBox.setVgrow({{PARENT}}, Priority.ALWAYS);

It fixed for me:

enter image description here

enter image description here

Feeney answered 27/1, 2021 at 12:14 Comment(0)
A
0

You could use a ScrollPane as the root of the scene and put everything else inside it. Then set the property setFitToWidth and setFitToHeight to true and all the content inside the ScrollPane will be stretched to fit the ScrollPane size and the ScrollPane will fit the Scene since its a Layout Pane. It will also show ScrollBars if the user resizes the window to be smaller than the contents minWidth, so the content doesnt get cut off!

@Override
public void start(Stage stage) {

    TableView<ObservableList<String>> table = new TableView<ObservableList<String>>();
    table.setMinWidth(400);

    ScrollPane sp = new ScrollPane(table);
    sp.setFitToHeight(true);
    sp.setFitToWidth(true);

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

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

I copied parts of the MCVE From Jonathan's answer, hope you dont mind Jonathan :)

For more general tips for making resizable GUIs check this post!

Agonic answered 12/8, 2016 at 12:39 Comment(1)
more like a workaround than a fixFishhook

© 2022 - 2024 — McMap. All rights reserved.