0

我有一个带有 popupMenu 的 JTable:

    final JPopupMenu popupMenu = new JPopupMenu();
    JMenuItem addItem = new JMenuItem("Add Movie");
    JMenuItem deleteItem = new JMenuItem("Delete Movie");

    addItem.addActionListener(new ActionListener() 
    {
        @Override
        public void actionPerformed(ActionEvent e) {
            model.addRow(new Movie("", 0, "", "", null, "DE"));
        }
    });
    deleteItem.addActionListener(new ActionListener() 
    {
        @Override
        public void actionPerformed(ActionEvent e) {
            Object frame = null;
            JOptionPane.showMessageDialog((Component) frame, "Movie wurde gelöscht");
        }
    });

    popupMenu.add(addItem);
    popupMenu.add(deleteItem);
    table.setComponentPopupMenu(popupMenu);

添加电影现在工作正常,但我也想删除电影(从我的列表中删除行+删除电影对象)当我点击“删除电影”时,我现在怎么知道光标在哪一行?我怎么做?

我试着这样做:

    deleteItem.addMouseListener( new MouseAdapter()
    {
        public void mouseReleased(MouseEvent e)
        {
                JTable source = (JTable)e.getSource();
                int row = source.rowAtPoint( e.getPoint() );
                int column = source.columnAtPoint( e.getPoint() );

                if (! source.isRowSelected(row))
                    source.changeSelection(row, column, false, false);
        }
    });

但我收到以下错误消息:

Exception in thread "AWT-EventQueue-3" java.lang.ClassCastException: javax.swing.JMenuItem cannot be cast to javax.swing.JTable

谢谢各位=)

4

0 回答 0