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

后端 未结 2 458
时光取名叫无心
时光取名叫无心 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-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());
            }
        }
    });
    

提交回复
热议问题