有没有办法更改 JDialog 的所有者?

发布于 2024-07-13 16:01:49 字数 125 浏览 8 评论 0原文

我有一个非常具体的问题,我想知道是否有办法更改 JDialog 的所有者(可以使用构造函数设置)。 我想不存在“官方”的可能性(除了黑客攻击),但我想确保我没有错过任何东西。 关于该主题的任何想法或提示都会有所帮助,谢谢......

I have a very specific problem, and I wanted to know if there is a way to change the owner of a JDialog (it can be set using the constructor). I suppose there is no "official" possibility (other than a hack), but I wanted to make sure I didn't miss something. Any ideas or hints on the topic would be helpful, thanks already...

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

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

发布评论

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

评论(3

烟织青萝梦 2024-07-20 16:01:49

如果您的问题是如何在应用程序生命周期中重用对话框,那么更好的方法是:

  1. 将所有对话框内容定义为 JPanel 子类
  2. ,并使用现有的 JPanel 子类实例实例化一个新的 JDialog

对于第 2 点,您当然可以使用惰性评估面板(仅在首次使用时实例化,然后再使用)。

您还需要让您的面板实现一些(您自己的)接口,允许您重新初始化它们以便在新的 JDialog 中重用(重新初始化通常意味着擦除所有字段内容,或将这些字段设置回默认值)。

If your question is about how to reuse dialogs during your application lifecycle, then a better way is to:

  1. define all your dialog contents as JPanel subclasses
  2. and instantiate a new JDialog with the existing JPanel subclass instance

For point 2, you can of course use lazy evaluation of the panels (instantiate upon first use only, then reuse).

You will also need to have your panels implement some interface (of your own) that allows you to re-initialize them for reuse in a new JDialog (reinit typically means erasing all fields contents, or setting these fields back to their default values).

肥爪爪 2024-07-20 16:01:49

我唯一能想到的就是不安全的黑客攻击(使用反射并改变所有者,但这可能会在不同版本的 JVM 下发生变化(即使来自同一平台上的同一供应商))。

也许您要问的更好的问题是“这就是我想要做的......我真的需要更改对话框的所有者还是有更好的方法”? 我正在尝试寻找想要更换所有者的理由,但我想不出任何理由......

Only thing I can think of falls under unsafe hack (use reflection and alter the owner, but that could possibly change under different version of the JVM (even from the same vensor on the same platform)).

Perhaps a better question for you to ask is "this is what I am trying to do... do I really need to change the owner of the dialog or is there a better way"? I am trying to think of reasons to want to change the owner and I cannot come up with any...

丶情人眼里出诗心の 2024-07-20 16:01:49

j:

当表格单元格中的文本太长而无法显示时,我也有类似的需求,需要在应用程序中使用单个“全局”对话框来提供“注释”对话框。 在这种情况下,用户可以将鼠标悬停或右键单击该单元格并获得一个显示整个文本的对话框。 但是,我希望对话框能够快速显示,尤其是悬停时,因此我想简单地显示和隐藏它。 我决定在主类中创建并存储此对话框,并以应用程序框架作为所有者。 然后,每当其所有者是框架的任何视图需要注释对话框时,该视图都可以从主类获取它。 在对话框需要注释对话框的情况下,父对话框可以使用适当的所有者创建注释对话框的新实例。 由于这些父对话框(至少在我的应用程序中)在关闭时被释放,因此额外的注释对话框实例将超出范围并被垃圾收集。 希望有帮助。

j:

I have had a similar need for a single "global" dialog in my application to serve a "notes" dialog when the text in a table cell is too long to display. In this case, the user can hover or right-click the cell and get a dialog that displays the whole text. However, I wanted the dialog to display quickly, especially when hovering, so I wanted to simply show and hide it. I decided to create and store this dialog in the main class with the application frame as the owner. Then, whenever any view whose owner was the frame needed the notes dialog, the view could get it from the main class. In those instances where a dialog needed the notes dialog, the parent dialog could create a new instance of the notes dialog with the appropriate owner. Since these parent dialogs (in my app at least) are disposed when closed, the extra notes dialog instance would be out of scope and garbage collected. Hope that helps.

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