我需要检测节点当前是否正在显示。即,如果我的节点位于 TabPane 中,我需要知道它是否位于选定的选项卡中。
在示例中,我想知道 HBox 何时显示。Node 的visibleProperty 和 ManagedProperty 似乎对我没有帮助:
public class VisibleTest extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
TabPane tabpane = new TabPane();
tabpane.getTabs().add(new Tab("Tab1", new Label("Label1")));
HBox hbox = new HBox(new Label("Label2"));
hbox.setStyle("-fx-background-color: aquamarine;");
hbox.visibleProperty().addListener((observable, oldValue, newValue) -> {
System.out.println("Hbox visible changed. newValue: " + newValue);
});
hbox.managedProperty().addListener((observable, oldValue, newValue) -> {
System.out.println("Hbox managed changed. newValue: " + newValue);
});
Tab tab2 = new Tab("tab2", hbox);
tabpane.getTabs().add(tab2);
primaryStage.setScene(new Scene(tabpane));
primaryStage.setWidth(600);
primaryStage.setHeight(500);
primaryStage.show();
} …Run Code Online (Sandbox Code Playgroud)