JTable, JPopupMenu and Mouse event
I am trying to add a JPopupMenu to a JTable. When the user right clicks on a row in the table I want the PopupMenu to be visible and at the same time I'd like to capture the row associated with the click. I am using this code to add the popup to the mouseListener:
baseTable.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
if(e.getButton()==MouseEvent.BUTTON3){
JMenuItem menuItem;
System.out.println(" row " + baseTable.getSelectedRow()+ " was clicked");
JPopupMenu popup = new JPopupMenu();
menuItem = new JMenuItem("Item 1");
//menuItem.addActionListener(this);
popup.add(menuItem);
menuItem = new JMenuItem("Item2");
//menuItem.addActionListener(this);
popup.add(menuItem);
menuItem = new JMenuItem("Item3");
//menuItem.addActionListener(this);
popup.add(menuItem);
int coordx = e.getX();
int coordy = e.getY();
popup.show(baseTable,coordx,coordy);
}
}
});
The problem with this is that the table has to first get focused before the popupmenu is shown and also before the row count is captured.
Any ideas how I can solve this?
Carlos

