【发布时间】:2014-10-09 21:18:54
【问题描述】:
在我的 JTable 中,我所有的复选框都在第 3 列中。单击按钮后,它将 删除那些被选中的行。
但是在我的 onclick 中实现了这些功能之后。它不会删除行。
按钮监听方法
class DeleteBtnListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0) {
for(int row = 0; row < table.getRowCount(); ++row) {
if((Boolean) table.getValueAt(row, 3) == true) {
myTableModel.removeRow(row);
table.revalidate();
table.repaint();
}
}
}
}
这就是我的 AbstractTableModel 类中的内容
@SuppressWarnings("serial")
class MyTableModel extends AbstractTableModel {
private final boolean DEBUG = true;
private String[] columnNames = {"Name",
"Age",
"Salary",
"Delete"};
private Object[][] data = {
{"Kathy", "20",new Integer(5), new Boolean(false)},
{"John", "35", new Integer(3), new Boolean(false)},
{"Sue", "20", new Integer(2), new Boolean(false)},
{"Jane", "12", new Integer(20), new Boolean(false)},
{"Mary", "42", new Integer(10), new Boolean(false)}
};
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
@SuppressWarnings("unchecked")
public Class<?> getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public boolean isCellEditable(int row, int col) {
return true;
}
public void setValueAt(Object value, int row, int col) {
if (DEBUG) {
System.out.println("Setting value at " + row + "," + col
+ " to " + value);
}
data[row][col] = value;
fireTableCellUpdated(row, col);
}
public void removeRow(int row) {
fireTableRowsDeleted(row, row);
}
}
【问题讨论】:
-
为了尽快获得更好的帮助,请发布MCVE(最小、完整、可验证的示例)。
-
为什么不使用
DefaultTableModel作为MyTableModel的基础?它可靠地添加和删除行。 -
好的,谢谢你的建议
标签: java swing jtable tablemodel