更新父 JFrame 时出现问题

发布于 2024-10-03 16:06:42 字数 381 浏览 0 评论 0原文

首先,让大家知道我是 java 的新手,它有一些怪癖。我是一位精通各种语言的经验丰富的程序员,这可能就是我陷入困境的原因...

我有一个应用程序,可能由于设计不佳,通过用户的工作流程生成了新的 JFrame。我的问题是,如果生成的 JFrame 中有一个事件,它是否能够联系并将数据或事件传递给它的父级?

我读过,使用 JDialog 似乎是设计的方式,但我们假设这不是一个选择。本质上,JFrame1 包含一个带有数据列表的 JTable。一个操作会生成 JFrame2,并且用户“执行某些操作”会影响 JFrame1 中列表中的数据。关闭JFrame2时,有没有办法根据JFrame2的关闭事件来控制JTable?

这是一个非常基本的概念,我似乎找不到允许这种操作的机制。

谢谢!

First, let it be known that I'm new to java and it's quirks. I'm a seasoned programmer with various languages, which may be why I'm stuck...

I have an application that, possibly due to poor design, spawns new JFrames through the users' work-flow. My question is, if there is an event in a spawned JFrame, is it able to contact and pass data or an event to it's parent?

I have read that using a JDialog seems to be the way to design, but let's assume that's not an option. Essentially, JFrame1 contains a JTable with a list of data. An action spawns JFrame2 and a user "does something" that impacts the data in the list in JFrame1. Upon closing JFrame2, is there a way to control the JTable based on JFrame2's close event?

It's a pretty basic concept, I just can't seem to find the mechanism that would allow such an action.

Thanks!

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

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

发布评论

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

评论(3

够钟 2024-10-10 16:06:42

您可以使用“监听器”来监听各种事件。

听起来您可能想从 如何编写窗口侦听器。

我读到,使用 JDialog 似乎是一种设计方式,但我们假设这不是一个选择。

为什么?代码是相同的,JDialogs 就是为此目的而设计的。需要使用 JFrame 的具体要求是什么?

一个操作会生成 JFrame2,并且用户“执行某些操作”会影响 JFrame1 中列表中的数据。关闭JFrame2时,有没有办法根据JFrame2的关闭事件来控制JTable?

这是一种常见的设计。用户选择要更改或更新的行,然后创建一个模型对话框来显示所有数据,以便可以更改。保存对话框时,表中的数据会更新。如果这是您的要求,那么您只需将 TableModel 传递到对话框即可。然后,当对话框关闭时,您更新 TableModel,表格将自动重新绘制。

You can use "listeners" to listen for various events.

It sounds like you might want to start with How to Write a Window Listener.

I have read that using a JDialog seems to be the way to design, but let's assume that's not an option.

Why? The code is the same and JDialogs where designed for this purpose. What is the specific requirement that says you need to use a JFrame?

An action spawns JFrame2 and a user "does something" that impacts the data in the list in JFrame1. Upon closing JFrame2, is there a way to control the JTable based on JFrame2's close event?

This is a common design. The user selects a row to change or update and a model dialog is created to display all the data so it can be changed. When the dialog is saved the data in the table is updated. If this is your requirement, then you can just pass in the TableModel to the dialog. Then when the dialog is closed you update the TableModel and the table will be repainted automatically.

╄→承喏 2024-10-10 16:06:42

您必须使用窗口侦听器捕获窗口关闭事件。窗口侦听器还需要对需要更改的数据的引用。

You would have to capture the window closing event using a window listener. The window listener would also need a reference to the data that needs to be changed.

不打扰别人 2024-10-10 16:06:42

除了在 JFrame 或 JDialog 上使用 Window.addWindowListener() 之外,还可以考虑使用模型视图方法。让关闭事件修改表的数据,而不是表本身。使用 AbstractTableModel 作为表的模型,并监听数据的更改。

In addition to using Window.addWindowListener() on either a JFrame or a JDialog, consider using a model-view approach. Have the close event modify the table's data, rather than the table itself. Use AbstractTableModel as the model for the table, and listen for changes to the data.

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