Search code examples
javaswingjtablejpopupmenu

Get jTable row number from popup item


I have a jTable as from the attached picture enter image description here

Right click on a row starts a jPopup, with a single item "Thread Stop".

I would like to return the row number by clicking on this menu item

How to accomplish this?

Thanks.


Solution

  • In your MouseListener where you show your popup, simply get the row and column numbers via the JTable methods:

      table.addMouseListener(new MouseAdapter() {
         @Override
         public void mousePressed(MouseEvent e) {
            Point p = e.getPoint();
            int row = table.rowAtPoint(p);
            int col = table.columnAtPoint(p);
    
            System.out.printf("row, col: [%d, %d]%n", row, col);
    
            // show pop-up menu here
    
         }
      });