添加内容后如何自动调整对话框窗格的大小? [英] How to automatically resize dialog pane when content is added?

查看:67
本文介绍了添加内容后如何自动调整对话框窗格的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我正在开发的应用程序,我需要用户输入许多行数据.确切的数字是可变的,用户也应该能够自己添加行.我现在已经使用JavaFX对话框来完成所有工作,除了以下事实:添加行时,对话框不会相应地调整大小.添加行时,是否可以让对话框自动调整大小?

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

}

推荐答案

要做

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

添加"按钮的动作事件处理程序.
Stage.sizeToScene()与Swing的jframe.pack()类似.对话框的底部被添加到某个(子,辅助)阶段,我们可以通过getScene().getWindow()来获得它.

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().

这篇关于添加内容后如何自动调整对话框窗格的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆