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

后端 未结 2 459
时光取名叫无心
时光取名叫无心 2021-01-12 23:50

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

相关标签:
2条回答
  • 2021-01-12 23:52

    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.

    0 讨论(0)
  • 2021-01-13 00:02
    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());
            }
        }
    });
    
    0 讨论(0)
提交回复
热议问题