Revision: 68996
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at April 1, 2015 23:20 by syedhussim
Initial Code
import javax.swing.*; import java.awt.event.*; import javax.swing.table.DefaultTableModel; class Program extends JFrame{ public static void main(String args[]){ Program main = new Program(); main.setVisible(true); } public Program(){ super("JTable Demo"); DefaultTableModel dtm = new DefaultTableModel( new Object[][] { { "1", "John", "Smith", "[email protected]" }, { "2", "Suzan", "Carter", "[email protected]" }, { "3", "Abdul", "Latif", "[email protected]" }, { "4", "Jia", "Lou", "[email protected]" } }, new Object[] { "Employee Id", "First Name", "Last Name", "Email" } ); JTable table = new JTable(dtm); ActionListener menuListener = new ActionListener() { public void actionPerformed(ActionEvent event) { DefaultTableModel dtm = (DefaultTableModel) table.getModel(); int selRow = table.getSelectedRow(); if(selRow > -1){ dtm.removeRow(selRow); } } }; table.addMouseListener(new MouseAdapter(){ public void mouseReleased(MouseEvent e) { if (e.isPopupTrigger() && e.getComponent() instanceof JTable ){ JPopupMenu popup = new JPopupMenu(); JMenuItem menuItem = new JMenuItem("Delete"); menuItem.addActionListener(menuListener); popup.add(menuItem); popup.show(e.getComponent(), e.getX(), e.getY()); } } }); this.getContentPane().add(table); this.setSize(400,400); } }
Initial URL
http://www.hostprojects.net/snippets/java/154/delete-selected-row-from-jtable-using-jpopupmenu
Initial Description
The following code snippet shows how to remove a row from a JTable component. The example uses a DefaultTableModel object to bind data to a JTable.
Initial Title
Delete Selected Row From JTable Using JPopupMenu
Initial Tags
Initial Language
Java