我想创建一个“通用”JPanel
,它可以在任何更高级别的 Window
类(JFrame
、JDialog< /code>,或者准确地说是 JInternalFrame
。)
我希望当该窗口不再在屏幕上呈现时能够“清理”一些静态值。通常,我会使用 Window
类(如 JFrame
)来完成此操作,并简单地使用适当的 windowClosing
或 addWindowListener
来完成此操作。创建 JFrame
期间的 code>windowClosed 方法。
由于我希望任何 Window 类都能够呈现此内容,因此我什至不知道要添加哪种侦听器,也不知道要将其添加到哪个窗口。
有没有一种方法可以“挂钩” JPanel 的实现和渲染,以便无论 Window
类渲染什么,我都可以添加关闭挂钩?
(我查看了 PropertyChangeListener
,但 componentShown
不会在 JPanel
渲染上触发。)
任何帮助将不胜感激。
I want to create a 'generic' JPanel
which can be rendered in any of the higher level Window
classes (JFrame
, JDialog
, or JInternalFrame
to be somewhat precise.)
I want to be able to 'clean up' some static values when that window is no longer being rendered on screen. Normally, I would do this with a Window
class (like JFrame
) and simply addWindowListener
with proper windowClosing
or windowClosed
methods during the creation of the JFrame
.
Since I desire any of the Window
classes to be able to render this, I don't even know which kind of listener to add nor do I know which window to add it to.
Is there a way to 'hook' the realization and rendering of the JPanel
to so that I can add my shutdown hooks no matter what Window
class renders it?
(I looked at PropertyChangeListener
, but componentShown
doesn't trigger on the JPanel
rendering.)
Any help would be appreciated.
发布评论
评论(2)
好吧,我终于找到了一些工作,但我不确定我是否真的喜欢这个答案。
我在创建时向
JPanel
添加了AncestorListener
。此侦听器消除了ancestorRemoved
和ancestorMoved
事件,并在ancestorAdded
事件中,将使用getTopLevelAncestor()
挂钩适当的WindowListener
/InternalFrameListener
以便我可以在窗口关闭时运行关闭代码。如果有更好的方法,我愿意。 (我将回顾 Geoff 的
HierarchyListener
。)Well, I finally got something working, but I'm not sure I really like the answer.
I added an
AncestorListener
to theJPanel
at creation. This listener stubbed out theancestorRemoved
andancestorMoved
events and in theancestorAdded
event, would hook thegetTopLevelAncestor()
with the appropriateWindowListener
/InternalFrameListener
so that I could run my shutdown code when the window closes.If there's a better way, I'm open to it. (I'm going to be reviewing the
HierarchyListener
from Geoff.)根据您想要的确切语义,有一些不同的选项。您可以注册一个
ComponentListener
并处理componentHidden
方法。另一种可能性是注册一个 HierarchyListener 并检查 DISPLAYABILITY_CHANGED 事件。您还可以使用 HierarchyListener 来查找何时在容器中添加或删除面板,并从旧窗口和新窗口中添加/删除窗口侦听器。ComponentListener
和HierarchyListener
之间的区别在于,ComponentListener
由任何可见性更改触发,而HierarchyListener
/< code>DISPLAYABILITY_CHANGED 事件在面板窗口被释放时触发。ComponentListener
可能是您最好的选择,但请注意,该面板将来可能会再次设置为可见。您还可以尝试
AncestorListener
ancestorRemoved
事件。如果组件本身或其任何祖先变得不可见,则会调用它。There's a few different options depending on the exact semantics you want. You can register a
ComponentListener
and handle thecomponentHidden
method. Another possibility is to register aHierarchyListener
and check forDISPLAYABILITY_CHANGED
events. You could also use aHierarchyListener
to find when the panel has been added or removed from a container and add/remove window listeners from the old and new window. The difference between theComponentListener
andHierarchyListener
is that theComponentListener
is triggered by any visibility change while theHierarchyListener
/DISPLAYABILITY_CHANGED
event is triggered when the panel's window is disposed.ComponentListener
is probably your best bet, but be aware that the panel might be set as visible again in the future.You can also try the
AncestorListener
ancestorRemoved
event. It's called if the component itself or any of it's ancestors is made invisibile.