- To add context menu you need to add mouse listener on jtable...
jTable1.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e)
{
int r = jTable1.rowAtPoint(e.getPoint());
if (r >= 0 && r < jTable1.getRowCount()) {
jTable1.setRowSelectionInterval(r, r);
} else {
jTable1.clearSelection();
}
//row index is found...
int rowindex = jTable1.getSelectedRow();
if (rowindex < 0)
return;
if (e.isPopupTrigger() && e.getComponent() instanceof JTable ) {
JPopupMenu popup = createYourPopUp(rowindex,jTable1);
popup.show(e.getComponent(), e.getX(), e.getY());
}
}
});
//here we are creating popup menu on right click of each row....
{
JPopupMenu popup=new JPopupMenu();
edit=new JMenuItem("Edit Details");
delete=new JMenuItem("Delete Details");
popup.add(edit);
popup.add(delete);
return popup;
}
Screenshots :
Download example :
Download
No comments:
Post a Comment