0

我正在尝试删除 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();
}

我做错了什么?

4

1 回答 1

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. 您正在依赖过时的信息...

更好的解决方案是使用某种后台进程来执行用户的删除,并在模型中提供一种方法来查找User对象本身并将其从模型中删除。这消除了您更改索引的可能性。

ASwingWorker提供了一种方法,我们可以通过它在后台执行操作,关闭事件调度线程,同时提供在需要时将所需操作(如修改表模型)重新同步回 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从模型中删除对象,但我没有你的模型,所以很难提出建议......

查看Swing 中的并发以获取更多详细信息...

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

更新了评论表格 TrashGod

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

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

查看排序和过滤以获取更多详细信息...

使用可运行示例更新

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();
        }            
    }
});
于 2013-09-23T23:39:34.887 回答