简体   繁体   English

编辑JTable中新添加的行

[英]Edit new added Row in JTable

i added a Row in my JTable with this 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) {
            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);

But alway, when i try to edit the values in this row i get an Error-Message. 但是总是,当我尝试编辑该行中的值时,我收到一条错误消息。 Does anybody know why? 有人知道为什么吗?

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)
...

Can you help me? 你能帮助我吗?

You probably didn't implement the addRow() method properly. 您可能没有正确实现addRow()方法。

You need to add the Movie object to your movie List and then invoke fireTableRowsInserted(...). 您需要将Movie对象添加到影片列表中,然后调用fireTableRowsInserted(...)。

Check out Row Table Model . 查看行表模型 The RowTableModel class will give you an idea of how to implement the addRow() method. RowTableModel类将使您了解如何实现addRow()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM