Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically resize dialog pane when content is added?

Tags:

java

javafx-8

For an application I am developing, I need the user to input a number of rows of data. The exact number is variable and the users should be able to add rows themselves too. I've got this all working now with a JavaFX Dialog, apart from the fact that when you add rows, the Dialog is not resized accordingly. Is there a way to get the Dialog to resize automatically when a row is added?

Below is a demo application with a sample test application with a Dialog similar to what I want.

package test_dialog;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.DialogPane;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Test_Dialog extends Application {

    class ScalableDialog extends Dialog<String> {

        int nrRows = 2;

        public ScalableDialog() {

            // We are resizable
            setResizable(true);

            // Set up the grid pane.
            GridPane grid = new GridPane();
            grid.setHgap(10);
            grid.setVgap(5);
            grid.setMaxWidth(Double.MAX_VALUE);
            grid.setAlignment(Pos.CENTER_LEFT);

            // Set up dialog pane
            DialogPane dialogPane = getDialogPane();
            dialogPane.setHeaderText(null);
            dialogPane.getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);
            dialogPane.setContent(grid);

            // Create some fields to start with
            for (int i = 0; i < nrRows; i++) {
                grid.addRow(i, new TextField("Row: " + i));
            }

            // Add button
            final Button buttonAdd = new Button("Add Row");
            buttonAdd.setOnAction((ActionEvent e) -> {
                // Move Button to next row
                GridPane.setRowIndex(buttonAdd, nrRows + 1);
                // Insert new text field row
                grid.addRow(nrRows, new TextField("New: " + nrRows++));
            });
            grid.add(buttonAdd, 0, nrRows);
        }
    }

    @Override
    public void start(Stage primaryStage) {
        Button button = new Button();
        button.setText("Open Dialog");
        button.setOnAction(e -> {
            new ScalableDialog().showAndWait();
        });

        StackPane root = new StackPane();
        root.getChildren().add(button);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Scalable Dialog Test");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}
like image 638
Hayo Baan Avatar asked Jul 03 '15 13:07

Hayo Baan


1 Answers

Do

dialogPane.getScene().getWindow().sizeToScene();

on action event handler of "Add" button.
Stage.sizeToScene(), is similar to Swing's jframe.pack(). At the bottom the dialog is being added to some (sub, secondary) stage, and we can get it by getScene().getWindow().

like image 165
Uluk Biy Avatar answered Oct 29 '22 00:10

Uluk Biy