JavaFX 8: Custom steps (window decoration thickness)?

How can I define the stage / window tab in JavaFX? In Swing, I could just write:

JFrame frame = new JFrame(); Insets insets = frame.getInsets(); 

What will be the equivalent in JavaFX to get the border size and window title?

+5
source share
1 answer

You can identify them by looking at the borders of the scene relative to the width and height of the window.

Given the Scene scene; , scene.getX() and scene.getY() , specify the x and y coordinates of the Scene in the window. They are equivalent to the left and top tabs, respectively.

The right and bottom are a little trickier, but

 scene.getWindow().getWidth()-scene.getWidth()-scene.getX() 

gives the correct insert and similarly

 scene.getWindow().getHeight()-scene.getHeight()-scene.getY() 

gives the bottom insert.

These values, of course, will only make sense after the scene is placed in the window and the window is visible on the screen.

If you really need an Insets object, you can do something like the following (which would even remain valid if the border or title bar changed after the window was displayed):

 import javafx.application.Application; import javafx.beans.binding.Bindings; import javafx.beans.binding.ObjectBinding; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class WindowInsetsDemo extends Application { @Override public void start(Stage primaryStage) { Label topLabel = new Label(); Label leftLabel = new Label(); Label rightLabel = new Label(); Label bottomLabel = new Label(); VBox root = new VBox(10, topLabel, leftLabel, bottomLabel, rightLabel); root.setAlignment(Pos.CENTER); Scene scene = new Scene(root, 600, 400); ObjectBinding<Insets> insets = Bindings.createObjectBinding(() -> new Insets(scene.getY(), primaryStage.getWidth()-scene.getWidth() - scene.getX(), primaryStage.getHeight()-scene.getHeight() - scene.getY(), scene.getX()), scene.xProperty(), scene.yProperty(), scene.widthProperty(), scene.heightProperty(), primaryStage.widthProperty(), primaryStage.heightProperty() ); topLabel.textProperty().bind(Bindings.createStringBinding(() -> "Top: "+insets.get().getTop(), insets)); leftLabel.textProperty().bind(Bindings.createStringBinding(() -> "Left: "+insets.get().getLeft(), insets)); rightLabel.textProperty().bind(Bindings.createStringBinding(() -> "Right: "+insets.get().getRight(), insets)); bottomLabel.textProperty().bind(Bindings.createStringBinding(() -> "Bottom: "+insets.get().getBottom(), insets)); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } } 
+5
source

Source: https://habr.com/ru/post/1206081/


All Articles