我用这个弹出菜单在我的 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) {
System.out.println("test");
Object frame = null;
JOptionPane.showMessageDialog((Component) frame, "Movie wurde hinzugefügt!");
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);
但总是,当我尝试编辑此行中的值时,我会收到一条错误消息。有人知道为什么吗?
Exception in thread "AWT-EventQueue-3" java.lang.IndexOutOfBoundsException: Invalid range
at javax.swing.DefaultRowSorter.rowsUpdated(DefaultRowSorter.java:896)
at javax.swing.DefaultRowSorter.rowsUpdated(DefaultRowSorter.java:915)
at javax.swing.JTable.notifySorter(JTable.java:4264)
at javax.swing.JTable.sortedTableChanged(JTable.java:4118)
at javax.swing.JTable.tableChanged(JTable.java:4395)
...
你能帮助我吗?