23

I want to resize a window to fit the contents of the window. In Swing there is the pack() method. Is there a similar method to do this in JavaFX?

What I am trying to do is to create a confirmation dialog. When I create the dialog, it is wider than the contents, so I was asking myself if I need something like the pack method.

Here is a screenshot of what is happening: enter image description here

And here is my code:

mainClass.getPrimaryStage().setOnCloseRequest(new EventHandler<WindowEvent>() {
    @Override
    public void handle(final WindowEvent e) {
        e.consume();

        final Stage dialog = new Stage();
        dialog.setTitle("Confirm Before Exit");
        dialog.setResizable(false);
        dialog.initOwner(mainClass.getPrimaryStage());
        dialog.initModality(Modality.APPLICATION_MODAL);

        FlowPane buttons = new FlowPane(10,10);
        buttons.setAlignment(Pos.CENTER);
        Button yes = new Button("Yes");
        Button no = new Button("No");
        buttons.getChildren().addAll(yes, no);
        VBox box = new VBox();
        box.setAlignment(Pos.CENTER);
        box.setSpacing(10);
        box.getChildren().addAll(new Label("Do you really want to exit?"), buttons);

        yes.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent t) {
                Platform.exit();
            }
        });
        no.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent t) {
                dialog.close();
            }
        });

        Scene s = new Scene(box);
        dialog.setScene(s);
        dialog.show();
    }
});

I hope they implement something like JOptionPane in JavaFX soon! This is not something that I should be doing, it is so basic...

Ky -
  • 30,724
  • 51
  • 192
  • 308
ceklock
  • 6,143
  • 10
  • 56
  • 78
  • You better pack it manual where possible, often constructions should better be png's for example. – Grim Dec 31 '12 at 09:44
  • @PeterRader please see my code above and tell me if there is something wrong. – ceklock Dec 31 '12 at 21:24
  • @tecnotron and also note that you have used box here.This is because of box. – joey rohan Jan 01 '13 at 10:00
  • @joeyrohan you are right, the problem was because VBox + FlowPane. – ceklock Jan 01 '13 at 21:17
  • Hi @joeyrohan @ceklock, may i know what's wrong with VBox + FlowPane combination? As I try to add `dialog.sizeToScene();` before `dialgo.show()`, it doesn't work for me. If not VBox + FlowPane combination, what is the equivalent combination I can use? Thanks. – Cheok Yan Cheng Jan 11 '15 at 09:57

1 Answers1

25

Try using sizeToScene() I think this is it what you want. Let me give an example:

JavaFX stage fitted to a button

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class NewFXMain extends Application {

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

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Hello World!");
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });

        StackPane root = new StackPane();
        root.getChildren().add(btn);
        primaryStage.setScene(new Scene(root));
        primaryStage.sizeToScene();
        primaryStage.show();
    }
}
Whymarrh
  • 13,139
  • 14
  • 57
  • 108
joey rohan
  • 3,505
  • 5
  • 33
  • 70
  • Thanks for the sizeToScene() tip. That is the method I was looking for. It is very good to use when the contents of the Stage are changed. – ceklock Jan 01 '13 at 21:19