简体   繁体   中英

Edit new added Row in JTable

i added a Row in my JTable with this popupmenu:

    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.

You need to add the Movie object to your movie List and then invoke fireTableRowsInserted(...).

Check out Row Table Model . The RowTableModel class will give you an idea of how to implement the addRow() method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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