我有一个使用自定义DefaultTableModel的JTable,该JTable在最后一列中显示了一些布尔值(显示为复选框)。
当我添加一个MouseListener来获取所单击内容的值时,似乎不再发生对复选框的切换。
// As soon as this is used in the component
// that is using the JTable, the toggling stops
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
int col = table.getSelectedColumn();
int row = table.getSelectedRow();
Object o = table.getModel().getValueAt(row, col);
我假设该事件正在由侦听器使用。我可以添加什么到MouseListener代码中以恢复切换行为?
编辑:
糟糕,问题似乎出在我的优先选择上:
@Override
public void setValueAt(Object aValue, int row, int column) {
// Attempt at mutually exclusive checkboxes
if( column == 2 ){ // Starts at 0. Seek an alternative solution to avoid hardcoding?
// Algorithm: cycle through List to set other Booleans to false
// Uses entities. Is there another way of getting the number of rows from model?
List<MyEntity> myEntities = this.getDatas();
for( int i = 0; i < myEntities.size(); i++ ){
if( i != row ){
// Make sure this calls parent
super.setValueAt( false , i, 2);
}
}
} else {
super.setValueAt(aValue, row, column); // Call parent class
}
}
相关分类