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.
- Here, we create a scene out of a Stack Pane and also set size of the stage.
- In this code snippet, we show the stage after setting the title and scene.
setOpacitymethod 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.
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:
- The functions
setMaxWidthsets 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.
- Here, we provided the height restriction by setting the upper and lower limit for the height via the function calls
When the stage is displayed to resize the stage and observe how it responds.
4. Code Reference
public class StageResize extends Application
public void start(Stage primaryStage)
//Sample 01: Create Scene
StackPane root = new StackPane();
Scene scene = new Scene(root, 300, 250);
//Sample 02: Set the Stage Opacity
//Sample 03: Set Resize Restriction
//Sample 04: Show Stage to User
public static void main(String args)