File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JavaFX and the fly likes Proportional resizing of the main window Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JavaFX
Bookmark "Proportional resizing of the main window" Watch "Proportional resizing of the main window" New topic

Proportional resizing of the main window

Ron Alby

Joined: May 31, 2011
Posts: 27
I would like to ensure that my main window always has the same height/width ratio. I was hoping there would be some sort of 'Resize' event, but I can't find one.

I can get separate callbacks on height changing and width changing - but trying to maintain proportions in two separate callbacks strikes me as being a lot more difficult (they can be making it larger or smaller, sometimes they change height, sometimes they change width, sometimes they change both at the same time).

Can anyone point me to a solution? Sorry if there is an obvious solution I'm just overlooking.

Darryl Burke

Joined: May 03, 2008
Posts: 5115

I don't think I know the answer, but anyone who might would first need to know whether your question pertains to JavaFX Script (aka 1.x) or JavaFX 2.

luck, db
There are no new questions, but there may be new answers.
Ron Alby

Joined: May 31, 2011
Posts: 27
Yeah ... sorry about that
Java 2.1-Beta on Ubuntu Linux
John Damien Smith
Ranch Hand

Joined: Jan 26, 2012
Posts: 227
See the sample code here on how to listen to stage (JavaFX term for most windows) size changes and modify the size of the stage if necessary:

Essentially, you can do something like this:

And the do the same for height.
Then bind the stageHeightProperty to a proportion of the stageWidthProperty, e.g.

I didn't try the exact code to get the exact result you are requesting, but by playing around using the tips provided you should be able to achieve the effect you need.
Ron Alby

Joined: May 31, 2011
Posts: 27
Unfortunately, I could not get that to work. Creating new simple double properties passing in the width in the constructor did not trigger the callback. When I used the actual stage properties it said they had to be read only properties. When I did a cast I got a cast exception.

I gave up on binding and just tried to change the size in the callback of the read only property ... it's funny, the callback kept getting the updated size but the size never changed on the screen.

At this point I'm about to set resize to false.
I agree. Here's the link:
subject: Proportional resizing of the main window
It's not a secret anymore!