简体   繁体   English

检测是否已隐藏Java Swing组件

[英]Detect if Java Swing component has been hidden

Assume we have the following Swing application: 假设我们有以下Swing应用程序:

    final JFrame frame = new JFrame();

    final JPanel outer = new JPanel();
    frame.add(outer);

    JComponent inner = new SomeSpecialComponent();
    outer.add(inner);

So in this example we simply have an outer panel in the frame and a special component in the panel. 因此,在此示例中,我们只在框架中有一个外部面板,在面板中有一个特殊组件。 This special component must do something when it is hidden and shown. 隐藏和显示此特殊组件时必须执行某些操作。 But the problem is that setVisible() is called on the outer panel and not on the special component. 但问题是setVisible()在外部面板上调用而不是在特殊组件上调用。 So I can't override the setVisible method in the special component and I also can't use a component listener on it. 所以我不能覆盖特殊组件中的setVisible方法,我也不能在其上使用组件监听器。 I could register the listener on the parent component but what if the outer panel is also in another panel and this outer outer panel is hidden? 我可以在父组件上注册监听器,但是如果外部面板也在另一个面板中并且这个外部外部面板被隐藏了怎么办?

Is there an easier solution than recursively adding component listeners to all parent components to detect a visibility change in SomeSpecialComponent? 有没有比向所有父组件递归添加组件侦听器以检测SomeSpecialComponent中的可见性更改更简单的解决方案?

Thanks aioobe for your answer - I got here via Google, looking for the same thing. 感谢aioobe的回答 - 我通过谷歌来到这里,寻找同样的事情。 :-) It's worth noting that Component.isShowing() does the same job as your amIVisible() though, so a revised code snippet (including a check on the nature of the HierarchyEvent ) might be: :-)值得注意的是Component.isShowing()与你的amIVisible()相同的工作,所以修改后的代码片段(包括检查HierarchyEvent的性质)可能是:

class SomeSpecialComponent extends JComponent implements HierarchyListener {

    public void addNotify() {
        super.addNotify();
        addHierarchyListener(this);
    }

    public void removeNotify() {
        removeHierarchyListener(this);
        super.removeNotify();
    }

    public void hierarchyChanged(HierarchyEvent e) {
        if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0)
            System.out.println("Am I visible? " + isShowing());
    }
}

To listen for this kind of events occuring in the hierarchy, you could do the following. 要监听层次结构中发生的此类事件,您可以执行以下操作。

class SomeSpecialComponent extends JComponent implements HierarchyListener {

    private boolean amIVisible() {
        Container c = getParent();
        while (c != null)
            if (!c.isVisible())
                return false;
            else
                c = c.getParent();
        return true;
    }

    public void addNotify() {
        super.addNotify();
        addHierarchyListener(this);
    }

    public void removeNotify() {
        removeHierarchyListener(this);
        super.removeNotify();
    }

    public void hierarchyChanged(HierarchyEvent e) {
        System.out.println("Am I visible? " + amIVisible());
    }

}

You could even be more precise in the treatement of HierarchyEvents. 您甚至可以更精确地处理HierarchyEvents。 Have a look at 看一下

http://java.sun.com/javase/6/docs/api/java/awt/event/HierarchyEvent.html http://java.sun.com/javase/6/docs/api/java/awt/event/HierarchyEvent.html

Have a look at the ComponentListener (or ComponentAdapter) 看看ComponentListener(或ComponentAdapter)

http://java.sun.com/docs/books/tutorial/uiswing/events/componentlistener.html http://java.sun.com/docs/books/tutorial/uiswing/events/componentlistener.html

http://docs.oracle.com/javase/8/docs/api/java/awt/event/ComponentListener.html http://docs.oracle.com/javase/8/docs/api/java/awt/event/ComponentListener.html

And specifically the method: 特别是方法:

void componentHidden(ComponentEvent e)
    Invoked when the component has been made invisible.

A complete solution would look something like: 一个完整的解决方案看起来像:

inner.addComponentListener(new ComponentAdapter() {
    public void componentHidden(ComponentEvent ce) {
        System.out.println("Component hidden!");
    }
});

If the actions that should be carried out upon hiding is tightly coupled with the SomeSpecialCompnent, I would suggest to let SomeSpecialComponent implement ComponentListener, and add itself as a listener for the ComponentEvents in its constructor. 如果隐藏时应该执行的操作与SomeSpecialCompnent紧密结合,我建议让SomeSpecialComponent实现ComponentListener,并将其自身添加为其构造函数中ComponentEvents的侦听器。

Another useful way (more related to add/remove operations and perhaps not suitable for your specific scenario) is to override addNotify() and removeNotify() . 另一种有用的方法(更多与添加/删除操作有关,可能不适合您的特定方案)是覆盖addNotify()removeNotify()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM