删除多行会冻结JTable

时间:2013-09-23 21:28:17

标签: java swing model jtable

我试图在Jtable中删除多行(每个例子,五十五个),但我只能删除一个(我在你问之前使用多个区间选择!),我觉得Jtable冻结了位。我的删除按钮:

deleteButton.addActionListener(new java.awt.event.ActionListener() {
 public void actionPerformed(java.awt.event.ActionEvent e){                                      
SwingUtilities.invokeLater(new Runnable() {
  public void run(){ 
int[] selectedRow = jTable.getSelectedRows(); 
 for(int j=0; j<selectedRow.length; j++){
                     Boolean state= (Boolean)jTable.getValueAt(selectedRow[j],10); 
                     if(state==true){//deleta the row
                         User u=model.getUsers(selectedRow[j]);
                         new UserDao().delete(u);
                         model.remove(selectedRow[j]);
                         numberField.setText(String.valueOf(model.getRowCount()));
                     }
                 }
                     }
   });               
 }
});

我的删除:

public void remove(int row) {
this.userlist.remove(row);
this.fireTableDataChanged();
}

我做错了什么?

1 个答案:

答案 0 :(得分:2)

让我们仔细看看代码......

deleteButton.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent e){     
        // Don't know why you need to use invokeLater
        // The event should be trigged within the EDT if the user
        // clicked the button.  This may introduce a small "pause"
        // depending on what else is in the EDT...
        SwingUtilities.invokeLater(new Runnable() {
            public void run(){ 
                // Get the indices of the selected rows...okay
                int[] selectedRow = jTable.getSelectedRows(); 
                // Loop through the selected rows...good...
                for(int j=0; j<selectedRow.length; j++){
                    // Get the "state" of the row...okay
                    Boolean state= (Boolean)jTable.getValueAt(selectedRow[j],10); 
                    // Long winded if, but okay...
                    if(state==true){//deleta the row
                        // Don't know what's going on here,
                        // But I assume you are trying to delete
                        // something from some kind of database
                        // THIS is likely to chew up some time...
                        User u=model.getUsers(selectedRow[j]);
                        new UserDao().delete(u);
                        // Uh oh...
                        // If you remove a row from the model, the list of indices you
                        // have is now invalid, as they no longer point
                        // to the correct rows in the model
                        model.remove(selectedRow[j]);
                        numberField.setText(String.valueOf(model.getRowCount()));
                    }
                }
            }
        });               
    }
});

因此。两个问题。

  1. 您似乎在EDT的上下文中调用某种管理功能,这些功能“看起来”会导致EDT在很短的时间内变慢/暂停...
  2. 您依赖于过时信息......
  3. 更好的解决方案是使用某种后台进程来执行用户删除,并在模型中提供一种方法来查找User对象本身并将其从模型中删除。这消除了在你下面改变指数的可能性。

    SwingWorker提供了一种方法,通过该方法我们可以在事件调度线程之外的后台执行操作,同时提供在需要时将所需操作(如修改表模型)重新同步到EDT的方法...

    例如......

    public class DeleteUsersWorker extends SwingWorker<List<User>, User> {
    
        private UserTableModel model;
        private List<User> users;
    
        public DeleteUsersWorker(UserTableModel model, List<User> users) {
            this.model = model;
            this.users = users;
        }
    
        protected List<User> doInBackground() {
            UserDao dao = new UserDao();
            for (User user : users) {
                dao.delete(user);
                publish(user);
            }
            return users;
        }
    
        protected void process(List<User> users) {
            for (User user : users) {
                model.remove(user);
            }
        }
    }
    

    actionPerformed方法的内容......

    int[] selectedRow = jTable.getSelectedRows(); 
    List<User> usersToBeRemoved = new ArrayList<>(selectedRow.length);
    for(int row : selectedRow){
        // Is state part of the User object??
        Boolean state = (Boolean)jTable.getValueAt(row,10); 
        if(state){
            usersToBeRemoved.add(model.getUsers(row));
        }
    }
    DeleteUsersWorker worker = new DeleteUsersWorker(model, users);
    worker.execute();
    

    这可能需要在表模型中添加一些额外的功能以支持从模型中删除User对象,但我没有你的模型,因此很难提出建议......

    请查看Concurrency in Swing了解更多详情......

    更好的解决方案可能是在你的dao API上有一个监听器,它可以提供有关更新的通知,这样模型可以自行更新,但同样,没有足够的上下文来做出决定;)

    更新了评论表格TrashGod

    您还应该注意视图索引并不总是直接映射到模型索引。在对表进行排序或筛选时会发生这种情况。虽然你可能会争辩说你的表没有(排序或过滤),但最好不要做出这样的假设......

    从表中获取行索引时,应调用JTable#convertRowIndexToModel(int),它将返回模型中的索引点

    请查看Sorting and Filtering了解更多详情......

    使用可运行示例进行更新

    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.SwingWorker;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    import javax.swing.table.AbstractTableModel;
    
    public class TableDeleteRowsTest {
    
        public static void main(String[] args) {
            new TableDeleteRowsTest();
        }
    
        public TableDeleteRowsTest() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    final UserTableModel model = new UserTableModel(
                            new User("Kermit"),
                            new User("Fozzie"),
                            new User("Animal"),
                            new User("Miss Piggy"),
                            new User("Gonzo"),
                            new User("Beaker"),
                            new User("Crazy Harry"),
                            new User("Floyd Pepper"),
                            new User("Sweetums"));
    
                    final JTable table = new JTable(model);
    
                    JButton delete = new JButton("Delete");
                    delete.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            int[] selectedRows = table.getSelectedRows();
                            if (selectedRows.length > 0) {
                                List<User> users = new ArrayList<>(selectedRows.length);
                                for (int row : selectedRows) {
                                    int modelRow = table.convertRowIndexToModel(row);
                                    Boolean selected = (Boolean) model.getValueAt(modelRow, 1);
                                    if (selected) {
                                        users.add(model.getUser(modelRow));
                                    }
                                }
                                if (users.size() > 0) {
                                    new DeleteUserWorker(users, model).execute();
                                }
                            }
                        }
                    });
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new JScrollPane(table));
                    frame.add(delete, BorderLayout.SOUTH);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class DeleteUserWorker extends SwingWorker<List<User>, User> {
    
            private List<User> users;
            private UserTableModel model;
    
            public DeleteUserWorker(List<User> users, UserTableModel model) {
                this.users = users;
                this.model = model;
            }
    
            @Override
            protected void process(List<User> chunks) {
                for (User user : users) {
                    model.remove(user);
                }
            }
    
            @Override
            protected List<User> doInBackground() throws Exception {
                for (User user : users) {
                    // Simulated delay
                    Thread.sleep(250);
                    publish(user);
                }
                return users;
            }
    
        }
    
        public class UserTableModel extends AbstractTableModel {
    
            private List<User> users;
            private List<Boolean> selected;
    
            public UserTableModel(User... users) {
                this.users = new ArrayList<>(Arrays.asList(users));
                selected = new ArrayList<>(this.users.size());
                for (User user : this.users) {
                    selected.add(new Boolean(false));
                }
            }
    
            public User getUser(int row) {
                return users.get(row);
            }
    
            @Override
            public int getRowCount() {
                return users.size();
            }
    
            @Override
            public int getColumnCount() {
                return 2;
            }
    
            @Override
            public String getColumnName(int column) {
                String name = "?";
                switch (column) {
                    case 0:
                        name = "User";
                        break;
                    case 1:
                        name = "";
                        break;
                }
                return name;
            }
    
            @Override
            public Class getColumnClass(int column) {
                Class type = String.class;
                switch (column) {
                    case 0:
                        type = String.class;
                        break;
                    case 1:
                        type = Boolean.class;
                        break;
                }
                return type;
            }
    
            @Override
            public Object getValueAt(int rowIndex, int columnIndex) {
                Object value = null;
                switch (columnIndex) {
                    case 0:
                        value = users.get(rowIndex).getName();
                        break;
                    case 1:
                        value = selected.get(rowIndex);
                        break;
                }
                return value;
            }
    
            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return columnIndex == 1;
            }
    
            @Override
            public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
                switch (columnIndex) {
                    case 1:
                        if (aValue instanceof Boolean) {
                            selected.set(rowIndex, (Boolean) aValue);
                            fireTableCellUpdated(rowIndex, columnIndex);
                        }
                        break;
                }
            }
    
            public void remove(User user) {
                int index = users.indexOf(user);
                if (index >= 0) {
                    selected.remove(index);
                    users.remove(user);
                    fireTableRowsDeleted(index, index);
                }
            }
        }
    
        public class User {
    
            private String name;
    
            public User(String name) {
                this.name = name;
            }
    
            public String getName() {
                return name;
            }
        }
    }
    

    更新了其他示例

    以上示例仅删除标记为AND的行。要删除所有标记的行,您需要更多类似......

    的内容
    JButton delete = new JButton("Delete");
    delete.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            List<User> users = new ArrayList<>(selectedRows.length);
            for (int row = 0; row < table.getRowCount(); row++) {
                int modelRow = table.convertRowIndexToModel(row);
                Boolean selected = (Boolean) model.getValueAt(modelRow, 1);
                if (selected) {
                    users.add(model.getUser(modelRow));
                }
            }
            if (users.size() > 0) {
                new DeleteUserWorker(users, model).execute();
            }            
        }
    });