如何使主窗口对表格单击做出反应
我是 Java swing 的新用户。当用户单击该行时,我需要能够创建一个包含行信息的弹出窗口。我设法将 mouseClick 事件反应合并到我的表类中,并且我有可用的行信息。如何通知主窗口有关该事件的信息,以便它可以显示对话框/弹出框?
I am a new user of Java swing. I need to be able to create a popup with row info when the user clicks on that row. I managed to incorporate the mouseClick event reaction in my table class, and I have the row info available. How can I notify the main window about the event so it can display the dialog box/popup box?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需调用主窗口上的方法即可执行操作
Just call a method on the main window to perform the action
有多种方法可以处理此问题:
1) 您可以让自定义表类在其上有一个自定义侦听器(观察者模式),然后每当单击发生时都会调用该侦听器
2) 您可以让表在主窗口上调用方法 -即作为表构造的一部分传入主窗口
3) 您可以将主窗口注册为表的侦听器(即鼠标侦听器)并让它处理事件。
我确信还有其他人。这些是我见过最常用的。根据所编写软件的大小、范围和意图,每种软件都有其优点和缺点。这是一个学校项目,一个为学习 Swing 而编写的玩具,还是一个长期、更大的项目?如果是后者,我建议查找模型视图控制器(MVC)架构讨论,因为根据我的经验,从长远来看,它可以使代码的维护变得更加容易。
祝你好运。
There are several ways to handle this:
1) You can have the custom table class have a custom listener on it (Observer Pattern) which it then calls whenever the click occurs
2) You can have the table call a method on the main window - i.e. pass in the main window as part of the construction of the table
3) You can have the main Window register as a listener to the table (i.e. a mouse listener) and have it handle the events instead.
There are others, I am sure. These are the ones I have seen most often used. Depending on the size, scope and intent of the software being written, each has it's merits and detriments. Is this a project for school, a toy being written to learn about Swing, or is it designed to be a longer term, larger project? If it is the latter, I would recommend looking up Model View Controller (MVC) architecture discussions, as it can make, long term, the maintenance of the code much easier, in my experience.
Good luck.
您可以这样做:
然后实现一个操作
myMenuAction
,其中使用表中的index
。You can do it like this:
And then implement an Action
myMenuAction
where you use theindex
from your table.