Robert
Robert

Reputation: 313

Stop resizing beyond screen resolution range (JavaFX)

I'm developing a JavaFX application (with Java version 8u51) and I made it resizable but the default behavior that I have right now is not the desired one. The problem is that I am able to resize it beyond the screen resolution range. In other words, I can make the Stage bigger than my screen. Is there a way to limit the resize to the current monitor resolution? I have to mention that I need this to work on any resolution and any screen size.

Upvotes: 0

Views: 1943

Answers (2)

perf coder
perf coder

Reputation: 197

Yes, but what if the user wants to resize it again...smaller, this action should be permitted.

In this case you need to set actual size of the stage when the app launched and maximum allowed resizable size.

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

    Scene scene = new Scene(new Pane());
    stage.setScene(scene);

    Rectangle2D bounds = Screen.getPrimary().getVisualBounds();

    stage.initStyle(StageStyle.DECORATED);
    stage.setX(bounds.getMinX());
    stage.setY(bounds.getMinY());
    stage.setWidth(bounds.getWidth());
    stage.setHeight(bounds.getHeight());
    stage.setMaxWidth(bounds.getWidth() * 2);
    stage.show();
}

In here:

stage.setWidth(bounds.getWidth());
stage.setHeight(bounds.getHeight());
stage.setMaxWidth(bounds.getWidth() * 2);

you may set the size you want. You may even set minimum size of the stage by stage.setMinWidth() and/or stage.setMinHeight().

Upvotes: 5

James_D
James_D

Reputation: 209684

The Screen API enables you to find the different physical screens available at runtime, their bounds (which includes their sizes), and resolution in dots per inch. Then you can set the maximum width and height on your stage accordingly.

(It's not really clear to me exactly what you mean by "resize it beyond the screen resolution range", but this should give you the API to implement what you are trying to do.)

Upvotes: 3

Related Questions