user10871691
user10871691

Reputation:

JavaFX: Allow window resizing in one direction only

One can change the resizeable state of a JavaFX stage using javafx.stage.Stage.setResizable(boolean) method.


QUESTION

Is there any possibility to allow resize only horizontally or only vertically?


There are methods like Stage.setMaxWidth, Stage.setMaxHeight, Stage.setMinWidth, Stage.setMinHeight but they can only be used to control resizing with fixed sized stages (by setting width = minWidth = maxWidth for example, to disallow horizontal resizing).

Upvotes: 0

Views: 565

Answers (1)

Miss Chanandler Bong
Miss Chanandler Bong

Reputation: 4258

You can prevent external attempts to change height like this:

stage.show();
stage.maxHeightProperty().bind(stage.heightProperty());
stage.minHeightProperty().bind(stage.heightProperty());

Same for width:

stage.maxWidthProperty().bind(stage.widthProperty());
stage.minWidthProperty().bind(stage.widthProperty());

This way will give you the ability to resize it internally when the content changes (call sizeToScene() for example).

Upvotes: 5

Related Questions