0

i have a problem with the dialog size. After the program-window, i open another dialogs. The dialogs has a size. This works with Windows, Linux with KDE and older Gnome (Ubuntu 22.04) but not with the actual GNOME (Ubuntu 24.04 or Manjaro). When the comment out lines are active, it works better in actual GNOME but also not always. I have amount of things tryed, but nothing works. Any idea?

This is a sample code with the minimal things to show the problem:

public class Muster extends Application {

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

    @Override
    public void init() throws Exception {
    }

    @Override
    public void start(Stage primaryStage) {
        show(primaryStage, null, "Dialog 1", false, 500, 500);
        show(null, primaryStage, "Dialog 2", false, 400, 400);
        show(null, primaryStage, "Dialog 3", false, 400, 400);
    }

    private static void show(Stage primaryStage, Stage owner, String title, boolean wait, int pos, int size) {
        VBox vBox = new VBox();
        Label lbl = new Label("Das ist ein Dialog");
        Button btn = new Button("Dialog");
        btn.setOnAction(a -> show(null, owner, "Wieder einer", true, 600, 600));
        vBox.getChildren().add(lbl);
        vBox.getChildren().add(btn);
        vBox.setAlignment(Pos.CENTER);

        try {
            Stage stage = primaryStage == null ? new Stage() : primaryStage;
            Scene scene = new Scene(vBox, size, size);
            stage.setScene(scene);
            stage.setTitle(title);
            if (owner != null) {
                stage.initOwner(owner);
            }

            stage.setWidth(size);
            stage.setHeight(size);
            stage.setX(pos);
            stage.setY(pos);

//            stage.setOnShowing(e -> {
//                stage.setWidth(size);
//                stage.setHeight(size);
//                stage.setX(pos);
//                stage.setY(pos);
//            });
//            stage.setOnShown(e -> {
//                stage.setWidth(size);
//                stage.setHeight(size);
//                stage.setX(pos);
//                stage.setY(pos);
//            });

            stage.setOnCloseRequest(e -> {
                System.out.println("closing width: " + stage.getWidth());
                System.out.println("closing height: " + stage.getHeight());
                System.out.println("closing X: " + stage.getX());
                System.out.println("closing Y: " + stage.getY());
            });

            stage.requestFocus();
            stage.toFront();
            if (wait) {
                stage.showAndWait();

            } else {
                stage.show();
            }
        } catch (final Exception exc) {
            System.out.println(exc.getMessage());
        }
    }
}

2
  • Hello Tomás, to make sure they look the same on all systems, you must set Insets and specify the font and its size. Commented Jan 19 at 14:40
  • 1
    Usually, there is no reason to set the window size. Every control already has a preferred size, and every layout and Stage respects those preferred sizes. If you really need an exact size (and you probably don’t), you should pass the width and height to the Scene constructor, not the Stage constructor. Commented Jan 19 at 19:44

2 Answers 2

0

without size it also works only in KDE and older GNOME (22.04) and not in actual GNOME (24.04)

public class Muster extends Application {

    private static Stage primary = null;

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

    @Override
    public void init() throws Exception {
    }

    @Override
    public void start(Stage primaryStage) {
        primary = primaryStage;
        show(true, "Dialog 1", false);
    }

    private static void show(boolean first, String title, boolean wait) {
        VBox vBox = new VBox(10);
        vBox.setPadding(new Insets(25));
        vBox.setAlignment(Pos.CENTER);

        Button btnWait = new Button("Dialog wait");
        btnWait.setOnAction(a -> show(false, "Wieder einer", true));
        Button btn = new Button("Dialog");
        btn.setOnAction(a -> show(false, "Wieder einer", false));

        vBox.getChildren().add(new Label("Das ist ein Dialog"));
        vBox.getChildren().addAll(btnWait, btn);

        for (int i = 0; i < 10; ++i) {
            HBox hBox = new HBox(10);
            for (int ii = 0; ii < 10; ++ii) {
                Text text = new Text("Nummer: " + ii);
                Font font = Font.font("Verdana", FontWeight.BOLD, 16);
                text.setFont(font);
                hBox.getChildren().add(text);
            }
            vBox.getChildren().add(hBox);
        }

        try {
            Stage stage = first ? primary : new Stage();

            Scene scene = new Scene(vBox);
            stage.setScene(scene);
            stage.setTitle(title);
            if (!first) {
                stage.initOwner(primary);
            }

            stage.requestFocus();
            stage.toFront();
            if (wait) {
                stage.showAndWait();

            } else {
                stage.show();
            }
        } catch (final Exception exc) {
            System.out.println(exc.getMessage());
        }
    }
}

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

found the problem: In actual GNOME is the dialogs size only in "notResizeable" dialogs ok.

This works for me:

stage.setResizable(false);
scene.setOnMouseEntered(mouseEvent -> {
  Platform.runLater(() -> {
    stage.setResizable(true);
  }});
});

2 Comments

Not working properly for me. As soon as I move the mouse inside the scene, the size is back to minimum.
did you set a size? stage.setWidth(w); stage.setHeight(h);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.