我正在JTable
使用以下方法删除 a 的所有行:
myTableModel.getDataVector().removeAllElements();
myTableModel.setRowCount(0);
但是删除后它的足迹仍然存在!请检查此屏幕截图:
这只发生在我删除所有行时,如果只有一行就会出现问题!
为什么会发生这种情况?我该如何解决?
谢谢
使用 a DefaultTableModel
,您唯一的选择是使用模型提供的功能,removeRow
removeRow
将触发所需的适当事件来告诉JTable
它需要自我更新。桌子已经过优化,以改善绘画过程
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
public class TestTable {
public static void main(String[] args) {
new TestTable();
}
public TestTable() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
DefaultTableModel model = new DefaultTableModel();
JTable table = new JTable(model);
for (int index = 0; index < 10; index++) {
model.addColumn(index);
}
for (int row = 0; row < 1000; row++) {
String[] values = new String[10];
for (int col = 0; col < 10; col++) {
values[col] = row + "x" + col;
}
model.addRow(values);
}
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new JScrollPane(table));
frame.add(new JButton(new DeleteRowsFromDefaultModel(model)), BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class DeleteRowsFromDefaultModel extends AbstractAction {
private final DefaultTableModel model;
public DeleteRowsFromDefaultModel(DefaultTableModel model) {
this.model = model;
putValue(NAME, "Delete All");
}
@Override
public void actionPerformed(ActionEvent e) {
model.setRowCount(0);
}
}
}
如果您真的担心性能,您将需要创建自己的模型并创建自己的clear
方法来触发适当的事件。
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
public class TestTable1 {
public static void main(String[] args) {
new TestTable1();
}
public TestTable1() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
MyTabelModel model = new MyTabelModel();
JTable table = new JTable(model);
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new JScrollPane(table));
frame.add(new JButton(new DeleteRowsFromMyModel(model)), BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class DeleteRowsFromMyModel extends AbstractAction {
private final MyTabelModel model;
public DeleteRowsFromMyModel(MyTabelModel model) {
this.model = model;
putValue(NAME, "Delete All");
}
@Override
public void actionPerformed(ActionEvent e) {
model.clear();
}
}
public class MyTabelModel extends AbstractTableModel {
private List<String[]> rows;
public MyTabelModel() {
rows = new ArrayList<>(1000);
for (int row = 0; row < 1000; row++) {
String[] values = new String[10];
for (int col = 0; col < 10; col++) {
values[col] = row + "x" + col;
}
rows.add(values);
}
}
@Override
public int getRowCount() {
return rows.size();
}
@Override
public int getColumnCount() {
return 10;
}
@Override
public String getColumnName(int column) {
return String.valueOf(column);
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
String[] row = rows.get(rowIndex);
return row[columnIndex];
}
public void clear() {
int old = getRowCount();
if (old > 0) {
rows.clear();
fireTableRowsDeleted(0, old - 1);
}
}
}
}