gpt4 book ai didi

java - 删除多行卡住 JTable

转载 作者:行者123 更新时间:2023-12-02 06:42:49 30 4
gpt4 key购买 nike

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

我做错了什么?

最佳答案

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

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对象本身并将其从模型中删除。这消除了您下的索引被更改的可能性。

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 的评论

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

当从表中获取行索引时,您应该调用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();
}
}
});

关于java - 删除多行卡住 JTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18969212/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com