How to detect location and size of tab header in JavaFX

北慕城南 提交于 2019-12-06 06:05:59

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!