问题
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:
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.
来源:https://stackoverflow.com/questions/38366096/how-to-detect-location-and-size-of-tab-header-in-javafx