Calculate preferred size and set as minimum

Miroslav Nachev mnachev.nscenter.eu at gmail.com
Fri Aug 24 06:34:46 UTC 2018


Yes, it works. Thank you Kevin.
The used JDK is:

java version "10.0.2" 2018-07-17
Java(TM) SE Runtime Environment 18.3 (build 10.0.2+13)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.2+13, mixed mode)


Here is the code:
        Scene scene = new Scene(gridPane);
        scene.snapshot(null);
        double width = gridPane.getWidth();
        double height = gridPane.getHeight();
        System.out.println("width=" + width + ", height=" + height);
        if(width > 0 && height > 0) {
            primaryStage.setMinWidth(width + 1);
            primaryStage.setMinHeight(height + 1);
        }

        primaryStage.setScene(scene);
        primaryStage.setWidth(640);
        primaryStage.setHeight(480);

        primaryStage.show();



On Thu, Aug 23, 2018 at 8:47 PM Kevin Rushforth <kevin.rushforth at oracle.com>
wrote:

> In general, this is fragile and should not be relied upon (not to
> mention it is internal API that is not available in the latest release
> and is subject to change in 8u).
>
> One possibility using public API is to use Scene::snapshot, but even
> that isn't guaranteed to work in all cases if the Scene has never been
> shown.
>
> -- Kevin
>
>
> On 8/23/2018 10:34 AM, Miroslav Nachev wrote:
> > The GridPane is not empty:
> >          gridPane.add(sceneTitleHBox, 0, 0, 2, 1);
> >          Label usernameLabel = new Label("Username:");
> >          gridPane.add(usernameLabel, 0, 1);
> >
> >          TextField usernameText = new TextField();
> >          gridPane.add(usernameText, 1, 1);
> >
> >          Label passwordLabel = new Label("Password:");
> >          gridPane.add(passwordLabel, 0, 2);
> >
> >          PasswordField passwordField = new PasswordField();
> >          gridPane.add(passwordField, 1, 2);
> >          gridPane.add(buttonLoginHBox, 0, 4, 2, 1);
> >          gridPane.add(buttonLoginActionMessage, 1, 6);
> >
> >
> >
> > On Thu, Aug 23, 2018 at 8:22 PM David Grieve <david.grieve at oracle.com>
> > wrote:
> >
> >> If your GridPane is empty, it won't have any height or width unless you
> >> set min.
> >>
> >>
> >> On 8/23/18 1:16 PM, Miroslav Nachev wrote:
> >>> For Button this works. For GridPane doesn't works:
> >>>           gridPane.applyCss();
> >>>           gridPane.layout();
> >>>           double width = gridPane.getWidth();
> >>>           double height = gridPane.getHeight();
> >>>           System.out.println("width=" + width + ", height=" + height);
> >>> --- exec-maven-plugin:1.5.0:exec (default-cli) ---
> >>> width=0.0, height=0.0
> >>>
> >>>
> >>> On Thu, Aug 23, 2018 at 6:56 PM Shakir Gusaroff <
> >> shakir.gusaroff at gmail.com>
> >>> wrote:
> >>>
> >>>> . In javafx 8 you can find the width and height of the node before the
> >>>> Stage has been shown. Use applyCss() and layout().
> >>>>
> >>>>
> >>>>
> >>>>
> >>>>
> >>>>
> >>>>     @Override
> >>>>
> >>>>    public void start(Stage stage) throws Exception {
> >>>>
> >>>>
> >>>>
> >>>>       Group root = new Group();
> >>>>
> >>>>       Scene scene = new Scene(root);
> >>>>
> >>>>
> >>>>
> >>>>       Button button = new Button("Hello World");
> >>>>
> >>>>       root.getChildren().add(button);
> >>>>
> >>>>
> >>>>
> >>>>       root.applyCss();
> >>>>
> >>>>       root.layout();
> >>>>
> >>>>
> >>>>
> >>>>       double width = button.getWidth();
> >>>>
> >>>>       double height = button.getHeight();
> >>>>
> >>>>
> >>>>
> >>>>       System.out.println(width + ", " + height);
> >>>>
> >>>>
> >>>>
> >>>>       stage.setScene(scene);
> >>>>
> >>>>       stage.show();
> >>>>
> >>>>    }
> >>>>
> >>>>
> >>>> On Thu, Aug 23, 2018 at 10:35 AM Miroslav Nachev <
> >>>> mnachev.nscenter.eu at gmail.com> wrote:
> >>>>
> >>>>> Hi,
> >>>>>
> >>>>> Is there any standard way to calculate the preferred size for Scene,
> >>>>> Node/Pane and Stage/Window?
> >>>>> I want once a form/pane is visualized, it cannot be minimized to a
> >> certain
> >>>>> size. Since I did not find such a built-in opportunity, at the moment
> >> I am
> >>>>> doing the following:
> >>>>>
> >>>>>           Scene scene = new Scene(gridPane);
> >>>>>           primaryStage.setScene(scene);
> >>>>>           primaryStage.sizeToScene();
> >>>>>
> >>>>>           primaryStage.show();
> >>>>>           primaryStage.setMinWidth(primaryStage.getWidth() + 1);
> >>>>>           primaryStage.setMinHeight(primaryStage.getHeight() + 1);
> >>>>>           primaryStage.setWidth(640);
> >>>>>           primaryStage.setHeight(480);
> >>>>>           primaryStage.show();
> >>>>>
> >>>>> The first call to primaryStage.show() (in orange) I do to calculate
> the
> >>>>> minimum panel size (Scene/GridPane), then set that size as minimum on
> >>>>> primaryStage, and finally set the size I want and visualize the panel
> >>>>> again.
> >>>>>
> >>>>> The unpleasant side effect is, that the panel is no longer at the
> >> center
> >>>>> of
> >>>>> the screen.
> >>>>>
> >>>>> Any ideas?
> >>>>>
> >>>>>
> >>>>> Regards,
> >>>>> Miro.
> >>>>>
> >>
>
>


More information about the openjfx-dev mailing list