How to detect location and size of tab header in J

2019-05-19 06:15发布

I am still new to javaFX. In my code I need to get location and size of tab header, but I can't find any property or function which return its size or location.

1条回答
小情绪 Triste *
2楼-- · 2019-05-19 06:35

I don't understand your question properly, but generally you can use the lookup function to get the .tab-header-area CSS selector which is actually a StackPane.

TabPane tabPane = new TabPane();
tabPane.getTabs().addAll(new Tab("Tab1"), new Tab("Tab2"), new Tab("Tab3"));

Button b = new Button("Get header");
b.setOnAction((e) -> {  
    StackPane headerArea = (StackPane) tabPane.lookup(".tab-header-area");
    System.out.println("Coordinates relatively to Scene: " + headerArea.localToScene(headerArea.getBoundsInLocal()));
});

The output

Coordinates relatively to Scene: BoundingBox [minX:0.0, minY:0.0, minZ:0.0, width:500.0, height:29.0, depth:0.0, maxX:500.0, maxY:29.0, maxZ:0.0]

If you want to get the information about the individual tabs:

Set<Node> tabs = tabPane.lookupAll(".tab");
for (Node node : tabs) {
    System.out.println("Coordinates relatively to Scene: " + node.localToScene(node.getBoundsInLocal()));
}

and the output

Coordinates relatively to Scene: BoundingBox [minX:5.0, minY:5.0, minZ:0.0, width:54.0, height:24.0, depth:0.0, maxX:59.0, maxY:29.0, maxZ:0.0]
Coordinates relatively to Scene: BoundingBox [minX:59.0, minY:5.0, minZ:0.0, width:38.0, height:24.0, depth:0.0, maxX:97.0, maxY:29.0, maxZ:0.0]
Coordinates relatively to Scene: BoundingBox [minX:97.0, minY:5.0, minZ:0.0, width:38.0, height:24.0, depth:0.0, maxX:135.0, maxY:29.0, maxZ:0.0]

I have used the localToScene function with the parameter of the local bounds of the StackPane to get the coordinates relative to the Scene.

Note: Lookup is not guaranteed to work, until CSS has been applied to the Scene.

查看更多
登录 后发表回答