Toti
Toti

Reputation: 75

How to make pane and elements in it resizable when stage size is changing

Can you please tell me how can I realize that the whole content of a pane will be resized while the stage is resized with mousedragg. Here is my code:

public class fab extends Application {
private Stage stage;
private Pane pane;
private Scene scene;


@Override
public void start(Stage stage) throws Exception {

this.stage = stage;

Button button = new Button("Button");

pane = new Pane();
pane.getChildren().add(button);

stage.setTitle("Test");
scene = new Scene(pane, 640, 640);
stage.setScene(scene);
stage.show();

}

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

I think there is the idea of binding. But I don't know how to use that, in order to make all nodes of a pane resizable, when the stage size is changing.

I'm searching a solution without Fxml or sceneBuilder.

Thank you in advance.

Upvotes: 0

Views: 1376

Answers (2)

Matt
Matt

Reputation: 3187

Here is the solution if you wish to bind the width of the button to you scene width

button.minWidthProperty().bind(scene.widthProperty());

You can also modify this +/- whatever you want for ex

button.minWidthProperty().bind(scene.widthProperty().subtract(20));

and you can do the same for the height

button.minHeightProperty().bind(scene.heightProperty().subtract(200));

Upvotes: 1

Muzib
Muzib

Reputation: 2581

If you insists to use the Pane container then after the line scene = new Scene(pane, 640, 640); add this:

scene.widthProperty().addListener((c,o,n)->button.setPrefWidth((Double)n));
scene.heightProperty().addListener((c,o,n)->button.setPrefHeight((Double)n));

and after the line stage.setScene(scene); add this:

button.setPrefSize(scene.getWidth(), scene.getHeight());

This works fine with Pane and do your required thing.

But I prefer using an AnchorPane container and set the Top, Right,Bottom and Left anchors to 0 .

Upvotes: 2

Related Questions