jTable right-click popup menu

前端 未结 3 1637
温柔的废话
温柔的废话 2021-02-05 07:04

I have a SQL database and I am working on a program that will allow me to add/delete/modify records. I already managed to add records I am working on editing/deleting them.

3条回答
  •  再見小時候
    2021-02-05 07:33

    Here is an example on how to do that. The easiest way to achieve this, is to set a JPopupMenu on the JTable directly.

    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Arrays;
    import java.util.Vector;
    
    import javax.swing.JFrame;
    import javax.swing.JMenuItem;
    import javax.swing.JOptionPane;
    import javax.swing.JPopupMenu;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.SwingUtilities;
    
    public class TestTableRightClick {
    
        protected void initUI() {
            final JFrame frame = new JFrame(TestTableRightClick.class.getSimpleName());
            Vector columns = new Vector(Arrays.asList("Name", "Age"));
            Vector> data = new Vector>();
            for (int i = 0; i < 50; i++) {
                Vector row = new Vector();
                for (int j = 0; j < columns.size(); j++) {
                    row.add("Cell " + (i + 1) + "," + (j + 1));
                }
                data.add(row);
            }
            final JTable table = new JTable(data, columns);
            final JPopupMenu popupMenu = new JPopupMenu();
            JMenuItem deleteItem = new JMenuItem("Delete");
            deleteItem.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    JOptionPane.showMessageDialog(frame, "Right-click performed on table and choose DELETE");
                }
            });
            popupMenu.add(deleteItem);
            table.setComponentPopupMenu(popupMenu);
            frame.add(new JScrollPane(table), BorderLayout.CENTER);
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new TestTableRightClick().initUI();
                }
            });
        }
    }
    

    In case you want to automatically select the row where the right-click was made, add the following snippet:

    popupMenu.addPopupMenuListener(new PopupMenuListener() {
    
                @Override
                public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            int rowAtPoint = table.rowAtPoint(SwingUtilities.convertPoint(popupMenu, new Point(0, 0), table));
                            if (rowAtPoint > -1) {
                                table.setRowSelectionInterval(rowAtPoint, rowAtPoint);
                            }
                        }
                    });
                }
    
                @Override
                public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
                    // TODO Auto-generated method stub
    
                }
    
                @Override
                public void popupMenuCanceled(PopupMenuEvent e) {
                    // TODO Auto-generated method stub
    
                }
            });
    

提交回复
热议问题