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

2

6

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.

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.

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.

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.

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

Penguin answered 8/11, 2010 at 2:28 Comment(1)
possible duplicate of Java Swing JTable; Right Click Menu (How do I get it to "select" aka highlight the row)Martguerita
M
14
JTable.rowAtPoint(...);

You can get the point from the 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());
        }
    }
});
Muleteer answered 8/11, 2010 at 2:35 Comment(2)
Well, part of the problem is that there is no MouseEvent at the place in the code where I want this information. The MouseEvent was consumed in order to show the popup menu at the right place; the event that's just happened is an ActionEvent, generated when the user chose a menu option. So there's no X,Y connected with the event. I can save the MouseEvent (or the X,Y) and pass it to my event in various ways, but I was looking for a 'cleaner' way.Penguin
@user492820, the problem is that your code is structured wrong. The "cleaner way" is to select the row WHEN the popup is displayed. See my edit for the general code to do this.Muleteer
T
2

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.

Thaumatrope answered 8/11, 2010 at 7:51 Comment(3)
No, I don't want to select it; I think that's non-standard behavior, at least on a Windows box. I was hoping for a way to find out without that.Penguin
i believe it is a standard behavior to select a cell or a row on which the right click was performed. If you don't want to select the row, save row number in some parameter accessible from your menu listener.Thaumatrope
Well, little known facts about supposedly-well-known operating systems. I don't ever use right-click to select something, and managed to forget that it did so. Thanks, I'll use that (and camickr has shown the code for it in the comment below. Thanks to all.Penguin

© 2022 - 2024 — McMap. All rights reserved.