Does size matter? That’s a question that’s been around for a while and the answer is up for debate. But this is JavaFX and the answer is a resounding yes size does matter. I’m not saying that user interfaces need to bigger to be better, I’m talking about controlling size. If you don’t control it properly you will end up having undesirable results, what  if the user makes the window so small that part of the display gets cut out never to be seen again, unless the window is made larger again of course. Maybe its resized so large that there is loads of empty space around the edge of the interface. So your going to need to keep size under control and your in luck as in this post I will cover some ways to do this.

javafx-size-example-1
Not controlling size

I am going to be reusing some of the code that I wrote in previous posts, so have a look at Getting Started with JavaFX and JavaFX Charts look pretty good! for explanations of some of the code that will be used here.

A good place to start is controlling the minimum and maximum size of the window, although controlling the maximum size is not as useful as you will probably want users to be able to full screen the application. The window is know as the Stage in terms of JavaFX so to control the min/max size of the window some properties on the Stage will need to be adjusted.

There’s not much to explain here, all you need to do is call the setMaxHeight, setMaxWidth, setMinHeight and setMinWidth methods and pass in the sizes that you want to restrict the Stage to. If you decide that you don’t want to restrict the maximum size then just remove the set max methods and its all done. Now removing the max size is not a big deal but not controlling the minimum size of the window could make your application act unexpectedly. Its not a definite requirement as users do not normally make the windows really small but if it is possible for them to do so then someone will inevitably do it, so you’ve been warned.

Maintaining the aspect ratio of the window is also possible with a few lines of code, although it acts unexpectedly when decreasing size, which is pretty annoying… So take this with a pinch of salt as I personally don’t like using it very much.

The lines to control the min/max size of the Stage have been removed as the code to maintain the aspect ratio overrides its function, you could try it for yourself and put them back in and increase / decrease the windows size and see that it doesn’t prevent you from doing it.

That’s enough about controlling the size of the window, your going to need to know how to setup the components inside of it as well or they might go crazy or disappear off the edge of the window. We’re going to need to use some re-sizable components to do this such as AnchorPane, Hbox and VBox and use some anchors to control them.

I know that’s a lot of code to take in so I’m going to break it down for you.

AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"

This piece of code is taken out of one of the HBox tags and anchors it to each side of the AnchorPane that is the HBox‘s parent. The “0.0” means that it is anchored to the edge of the parent so if this was made to be “5.0” then it is anchored to 5 units from each edge.

To make this a bit clearer.

AnchorPane.bottomAnchor="5.0"

This means the bottom of the HBox is anchored to be 5 units from the AnchorPane‘s bottom edge.

AnchorPane.leftAnchor="100.0"

And this one means that the left of the HBox is anchored to be 100 units from the left of the parent AnchorPane‘s left edge.

Some more properties are coming your way.

HBox.hgrow="ALWAYS"

Use this to make components inside a HBox grow horizontally when the HBox changes size, if you don’t want it to change then use…

HBox.hgrow="NEVER"

Components inside VBox’s can also use a very similar property but instead of changing horizontally they will grow or shrink vertically.

VBox.vgrow="ALWAYS"

The examples above have used ALWAYS and NEVER but you can also use the values INHERIT and SOMETIMES. They seem pretty straight forward where INHERIT will take the property of its parent and honestly I’ve not used the SOMETIMES option but I guess it sometimes changes the size…

The last thing of note in this example is the maximum height or width of the buttons.

maxHeight="Infinity"
maxWidth="Infinity"

Without these properties inside of the Button tags they will not grow to fit their Hbox or Vbox parent even though each Button has defined either the vgrow or hgrow property.

Adding this all together will leave you looking with a much nicer application.

JavaFX Size Example 2.PNG
Controlling size

That’s the end of this little tutorial. As you can see, size does matter. So go tell that to all your friends, just remember to tell them you are talking about JavaFX or they might get the wrong impression!

Here is a link to all the code if you just want to plug in and go.