获取 JavaFX 8 中节点的可见状态

Olz*_*zen 5 java javafx javafx-8

我需要检测节点当前是否正在显示。即,如果我的节点位于 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();
}

public static void main(String[] args) {
    launch(args);
}
}
Run Code Online (Sandbox Code Playgroud)

我知道,可以监听selectedProperty选项卡的状态,但这并不能解决我的真正问题。

Node.impl_isTreeVisible()做我想做的事,但这是一个废弃的 API。

有任何想法吗?

- - - - - - - - - - - - - - - - - - 更新 - - - - - - - --------
我意识到上面的代码示例并不能很好地解释我想要完成的任务。
下面是一些 Swing 代码,演示了我试图在 JavaFX 中完成的任务。检测 JComponent/Node 是否可见/显示,并根据该状态启动或停止后台进程。如果它是 javaFX 类,构造函数会是什么样子。

public class SwingVisible extends JComponent {

    String instanceNR;
    Thread instanceThread;
    boolean doExpensiveStuff = false;

    public SwingVisible(String instanceNR) {
        this.instanceNR = instanceNR;
        this.setLayout(new FlowLayout());
        this.add(new JLabel(instanceNR));

        instanceThread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    if (doExpensiveStuff) {
                        /*
                         * do expensive stuff.
                         */
                        System.out.println(instanceNR + " is visible " + isVisible());
                    }
                }
            }
        });

        /*
         * How to do this in FX?
         */
        addComponentListener(new ComponentAdapter() {
            @Override
            public void componentShown(ComponentEvent e) {
                if (!instanceThread.isAlive()) {
                    instanceThread.start();
                }
                doExpensiveStuff = true;
            }

            @Override
            public void componentHidden(ComponentEvent e) {
                doExpensiveStuff = false;
            }
        });
    }

    public static void main(String[] args) {    
        /*
         * This block represents code that is external to my library. End user
         * can put instances of SwingVisible in JTabbedPanes, JFrames, JWindows,
         * or other JComponents. How many instances there will bee is not in my
         * control.
         */
        JTabbedPane jtp = new JTabbedPane();
        jtp.add("tab1", new SwingVisible("1"));
        jtp.add("tab2", new SwingVisible("2"));
        jtp.add("tab3", new SwingVisible("3"));

        JFrame f = new JFrame("test");
        f.setContentPane(jtp);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(300, 300);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}
Run Code Online (Sandbox Code Playgroud)

选择 tab1 时的输出:

1 可见 true
1 可见 true
1 可见 true

...

选择 tab2 时的输出:

2 可见,正确
2 可见,正确
2 可见,正确
...

Sed*_*ick 2

更新了答案。

tab2.getContent().isVisible();
Run Code Online (Sandbox Code Playgroud)