如何使主窗口对表格单击做出反应

发布于 2024-08-31 21:08:55 字数 126 浏览 1 评论 0原文

我是 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 技术交流群。

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

发布评论

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

评论(3

一身仙ぐ女味 2024-09-07 21:08:55

只需调用主窗口上的方法即可执行操作

Just call a method on the main window to perform the action

笨笨の傻瓜 2024-09-07 21:08:55

有多种方法可以处理此问题:

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.

仅一夜美梦 2024-09-07 21:08:55

您可以这样做:

myTable.addMouseListener(new MouseAdapter() {
        public void mouseReleased(MouseEvent e) {
            if(SwingUtilities.isRightMouseButton(e)) {
                int index = myTable.rowAtPoint(e.getPoint());
                JPopupMenu popup = new JPopupMenu();
                popup.add(myMenuAction);
                popup.show(e.getComponent(), e.getX(), e.getY());
            }
        }
});

然后实现一个操作 myMenuAction ,其中使用表中的 index

You can do it like this:

myTable.addMouseListener(new MouseAdapter() {
        public void mouseReleased(MouseEvent e) {
            if(SwingUtilities.isRightMouseButton(e)) {
                int index = myTable.rowAtPoint(e.getPoint());
                JPopupMenu popup = new JPopupMenu();
                popup.add(myMenuAction);
                popup.show(e.getComponent(), e.getX(), e.getY());
            }
        }
});

And then implement an Action myMenuAction where you use the index from your table.

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