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.
Explanation
- 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.
- 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.
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:
Explanation
- The functions
setMinWidth
andsetMaxWidth
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. - 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
package javafxexamples; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.StackPane; import javafx.stage.Stage; public class StageResize extends Application { @Override 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 primaryStage.setOpacity(0.75); //Sample 03: Set Resize Restriction primaryStage.setMinWidth(300); primaryStage.setMinHeight(250); primaryStage.setMaxWidth(600); primaryStage.setMaxHeight(400); //Sample 04: Show Stage to User primaryStage.setTitle("Stage Resize"); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } } |
Categories: JavaFx