Detect if Java Swing component has been hidden

后端 未结 3 761
离开以前
离开以前 2021-02-05 07:49

Assume we have the following Swing application:

    final JFrame frame = new JFrame();

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

    JCompon         


        
相关标签:
3条回答
  • 2021-02-05 08:07

    Thanks aioobe for your answer - I got here via Google, looking for the same thing. :-) 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:

    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());
        }
    }
    
    0 讨论(0)
  • 2021-02-05 08:12

    Have a look at the ComponentListener (or ComponentAdapter)

    http://java.sun.com/docs/books/tutorial/uiswing/events/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.

    Another useful way (more related to add/remove operations and perhaps not suitable for your specific scenario) is to override addNotify() and removeNotify().

    0 讨论(0)
  • 2021-02-05 08:18

    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. Have a look at

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

    0 讨论(0)
提交回复
热议问题