java 查找已调用弹出菜单的 JTable 行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4120862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 04:51:22  来源:igfitidea点击:

Find the JTable row on which a popup menu has been invoked

javaswingjtablejpopupmenu

提问by user492820

I have a JTable and a popup menu that is specific to each row. I want to calculate the row on which the user right-clicked his mouse (Windows L&F) to bring up the popup menu.

我有一个 JTable 和一个特定于每一行的弹出菜单。我想计算用户右键单击鼠标 (Windows L&F) 以显示弹出菜单的行。

I create a MouseListener for the table, so it gets the MouseEvent at the click, and shows the popup menu at the correct place. But when the user selects one item off the popup menu, I can't figure a way to determine what the row was where the user right-clicked in the first place. The event for the popup menu invocation doesn't have the x,y coordinates where the right-click took place any more.

我为表创建了一个 MouseListener,因此它在单击时获取 MouseEvent,并在正确的位置显示弹出菜单。但是当用户从弹出菜单中选择一个项目时,我无法找到一种方法来确定用户首先右键单击的行是哪一行。弹出菜单调用的事件不再具有发生右键单击的 x,y 坐标。

I've looked at getting the position of the popup, but that belongs to the frame, not the table, so neither it nor its parent have the right x,y values for what I want.

我已经查看了弹出窗口的位置,但它属于框架,而不是表格,因此无论是它还是它的父级都没有我想要的正确的 x,y 值。

I've worked around it by subclassing JPopupMenu and setting the x and y values I want it to have in the MouseListener. But it seems to me like this would be a general problem for anyone wanting to put a popup menu on a JTable, and I'm wondering what I've missed.

我已经通过继承 JPopupMenu 并设置我希望它在 MouseListener 中具有的 x 和 y 值来解决它。但在我看来,对于想要在 JTable 上放置弹出菜单的人来说,这将是一个普遍的问题,我想知道我错过了什么。

Is there a simpler way to do this, especially one that doesn't involve subclassing JPopupMenu?

有没有更简单的方法来做到这一点,尤其是不涉及子类化 JPopupMenu 的方法?

回答by camickr

JTable.rowAtPoint(...);

You can get the point from the MouseEvent.

您可以从 MouseEvent 中获得这一点。

Edit:

编辑:

table.addMouseListener( new MouseAdapter()
{
    public void mouseReleased(MouseEvent e)
    {
        if (e.isPopupTrigger())
        {
            JTable source = (JTable)e.getSource();
            int row = source.rowAtPoint( e.getPoint() );
            int column = source.columnAtPoint( e.getPoint() );

            if (! source.isRowSelected(row))
                source.changeSelection(row, column, false, false);

            popup.show(e.getComponent(), e.getX(), e.getY());
        }
    }
});

回答by Denis Tulskiy

If you don't mind selecting your row on right button click, then in the MouseListener use JTable.rowAtPoint()and select the row if it's not selected, and then in the popup listener use JTable.getSelectedRows()to process your rows. Or you can save them in a separate data structure that you can access from your popup menu listener.

如果您不介意在右键单击时选择行,则在 MouseListener 中使用JTable.rowAtPoint()并选择未选中的行,然后在弹出式侦听器中使用JTable.getSelectedRows()处理您的行。或者您可以将它们保存在单独的数据结构中,您可以从弹出菜单侦听器访问该数据结构。