如何将 JPanel 挂接到父/祖先容器的 windowClosing/Closed 侦听器中?

发布于 2024-08-19 20:38:38 字数 664 浏览 7 评论 0原文

我想创建一个“通用”JPanel,它可以在任何更高级别的 Window 类(JFrameJDialog< /code>,或者准确地说是 JInternalFrame。)

我希望当该窗口不再在屏幕上呈现时能够“清理”一些静态值。通常,我会使用 Window 类(如 JFrame)来完成此操作,并简单地使用适当的 windowClosingaddWindowListener 来完成此操作。创建 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

⒈起吃苦の倖褔 2024-08-26 20:38:38

好吧,我终于找到了一些工作,但我不确定我是否真的喜欢这个答案。

我在创建时向 JPanel 添加了 AncestorListener。此侦听器消除了 ancestorRemovedancestorMoved 事件,并在 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 the JPanel at creation. This listener stubbed out the ancestorRemoved and ancestorMoved events and in the ancestorAdded event, would hook the getTopLevelAncestor() with the appropriate WindowListener/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.)

瑾夏年华 2024-08-26 20:38:38

根据您想要的确切语义,有一些不同的选项。您可以注册一个 ComponentListener 并处理 componentHidden 方法。另一种可能性是注册一个 HierarchyListener 并检查 DISPLAYABILITY_CHANGED 事件。您还可以使用 HierarchyListener 来查找何时在容器中添加或删除面板,并从旧窗口和新窗口中添加/删除窗口侦听器。 ComponentListenerHierarchyListener 之间的区别在于,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 the componentHidden method. Another possibility is to register a HierarchyListener and check for DISPLAYABILITY_CHANGED events. You could also use a HierarchyListener 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 the ComponentListener and HierarchyListener is that the ComponentListener is triggered by any visibility change while the HierarchyListener/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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文