Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to fix javafx - tableview size to current window size

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);
like image 742
Rakesh SKadam Avatar asked Aug 12 '16 07:08

Rakesh SKadam


4 Answers

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

}
like image 67
Jonatan Stenbacka Avatar answered Nov 09 '22 14:11

Jonatan Stenbacka


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

like image 25
S.Rumeshi Avatar answered Nov 09 '22 16:11

S.Rumeshi


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

like image 2
Roman Shubenko Avatar answered Nov 09 '22 15:11

Roman Shubenko


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!

like image 1
404KnowledgeNotFound Avatar answered Nov 09 '22 15:11

404KnowledgeNotFound