@eatSleepCode 写了@mKorbel 你能给出实现 setValueAt 方法的示例代码吗?
(使用的OP)DefaultTableModel的代码,
对于基于的代码,AbstractTableModel
需要保存通知程序的代码排序fireTableCellUpdated(rowIndex, columnIndex)
;,因为/否则不会重新绘制任何内容JTables view
,
这两个模型及其通知器之间有一些重要的区别,并且(我的观点)没有理由打扰和使用AbstractTableModel
基本的东西(99pct 的表模型)
. . . . . . . .
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
public class TableRolloverDemo {
private JFrame frame = new JFrame("TableRolloverDemo");
private JTable table = new JTable();
private String[] columnNames = new String[]{"Column"};
private Object[][] data = new Object[][]{{false}, {false}, {true}, {true},
{false}, {false}, {true}, {true}, {false}, {false}, {true}, {true}};
public TableRolloverDemo() {
final DefaultTableModel model = new DefaultTableModel(data, columnNames) {
private boolean ImInLoop = false;
@Override
public Class<?> getColumnClass(int columnIndex) {
return Boolean.class;
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
if (columnIndex == 0) {
if (!ImInLoop) {
ImInLoop = true;
Boolean bol = (Boolean) aValue;
super.setValueAt(aValue, rowIndex, columnIndex);
for (int i = 0; i < this.getRowCount(); i++) {
if (i != rowIndex) {
super.setValueAt(!bol, i, columnIndex);
}
}
ImInLoop = false;
}
} else {
super.setValueAt(aValue, rowIndex, columnIndex);
}
}
};
table.setModel(model);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JScrollPane(table));
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
TableRolloverDemo tableRolloverDemo = new TableRolloverDemo();
}
});
}
}