特定行的JTable popupMenu

时间:2013-11-10 11:24:14

标签: java swing jtable popupmenu

我有一个带有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

谢谢你们=)

0 个答案:

没有答案