JavaFX: Allow window resizing in one direction only

Eugen Covaci :

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).

M. S. :

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).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=12138&siteId=1