一个简单的示例显示未清除选择fireTableCellUpdated()
(由以下方式调用DefaultTableModel#setValueAt()
:
import java.awt.EventQueue;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class TestTable {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
final DefaultTableModel model = new DefaultTableModel(3, 3);
JTable table = new JTable(model);
table.setRowSelectionInterval(1, 1);
final Timer timer = new Timer(500, new ActionListener() {
private final Random random = new Random();
private int i;
@Override
public void actionPerformed(ActionEvent e) {
model.setValueAt(i++, random.nextInt(3), random.nextInt(3));
}
});
timer.start();
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosed(WindowEvent e) {
timer.stop();
}
});
frame.getContentPane().add(new JScrollPane(table));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}