我正在删除JTable
的所有行:
myTableModel.getDataVector().removeAllElements();
myTableModel.setRowCount(0);
但删除后其足迹仍然存在!请检查此截图:
只有当我删除所有行时才会发生这种情况,如果有一行甚至会出现问题! 为什么会这样?我该怎么办呢? 感谢
答案 0 :(得分:1)
使用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);
}
}
}
}