Sunday, January 15, 2017

How to create jTable right click popup menu Java Swing GUI

How to create jTable right click popup menu Java Swing GUI


jTable right-click popup menu Java Swing GUI - Mouse click
Java 2016
table_6.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e)
{
int r = table_6.rowAtPoint(e.getPoint());
if (r >= 0 && r < table_6.getRowCount()) {
table_6.setRowSelectionInterval(r, r);
} else {
table_6.clearSelection();
}

//row index is found...
int rowindex = table_6.getSelectedRow();
if (rowindex < 0)
return;
if (e.isPopupTrigger() && e.getComponent() instanceof JTable ) {
JPopupMenu popup = createYourPopUp(rowindex,table_6);
popup.show(e.getComponent(), e.getX(), e.getY());
}
}

});
jTable right-click popup menu Java Swing GUI - Method
Java 2016
public static JPopupMenu createYourPopUp(int rowindex, JTable table_6)
{
JPopupMenu popup=new JPopupMenu();
JMenuItem edit=new JMenuItem("Edit Details");
JMenuItem delete=new JMenuItem("Delete Details");
edit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null, "Edit scucessfuly");
}
});
delete.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null, "Delete scucessfuly");
}
});
popup.add(edit);
popup.add(delete);
return popup;
}






Available link for download