Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

JavaFx Restrict Stage Resize & Stage Opacity

1. Stage Opacity

We can create see-through JavaFx Stages using the Opacity property. This property accepts a double value which ranges from 0 to 1. The value can be changed or fetched using the function getOpacity and setOpacity methods.

With Opacity value 0 we can display a fully transparent window and when the value is 1, it is fully opaque. So, if we set a value of 0.5, we can say we have a semi-transparent JavaFx stage. Note, some platform may not support transparent windows & in that case, setting the opacity has no effect on the stage. Let us see an example for setting a transparent see-through window.

2. The setOpacity Method

The code below shows setting a 25% transparent JavaFx stage.

Fig 1. SetOpacity Method Usage
Fig 1. SetOpacity Method Usage

Explanation

  1. Here, we create a scene out of a Stack Pane and also set size of the stage.
  2. In this code snippet, we show the stage after setting the title and scene.
  3. The setOpacity method in our example takes a value of 0.75. This means, we are claiming 75% opaqueness. In other words, the stage is 25% transparent.

When you run the code, it displays the stage, and we can see-through it. Below is screenshot which was taken when stage is overlapping the NetBeans IDE.

Fig 2. Transparent JavaFx Stage
Fig 2. Transparent JavaFx Stage

3. Stage Resize Restriction

By default, a stage is resizable. But we can make it as a fixed size stage by calling the function setResizeable(false). This restricts the end-user resizing the stage but not the code. Sometimes, it is required to set resize limits for the stage so that we cannot resize the stage beyond certain limits. Now, have a look at the below code:

Fig 3. JavaFx Stage Resize Restriction
Fig 3. JavaFx Stage Resize Restriction

Explanation

  1. The functions setMinWidth and setMaxWidth sets the lower and upper bounds on the width side. When the stage is displayed, user cannot resize the stage beyond these limits. For example, they cannot stretch the stage so that its width goes beyond 600 pixels. The same way it is not possible to reduce the width of the stage below 300 pixels.
  2. Here, we provided the height restriction by setting the upper and lower limit for the height via the function calls setMinHeight & setMaxHeight.

When the stage is displayed to resize the stage and observe how it responds.

4. Code Reference

Categories: JavaFx

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.