Detect if Java Swing component has been hidden
Posted
by kayahr
on Stack Overflow
See other posts from Stack Overflow
or by kayahr
Published on 2010-04-28T08:31:03Z
Indexed on
2010/04/28
8:43 UTC
Read the original article
Hit count: 235
Assume we have the following Swing application:
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. So I can't override the setVisible method in the special component and I also can't use a component listener on it. 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 componentlisteners to all parent components to detect a visibility change in SomeSpecialComponent?
© Stack Overflow or respective owner