JTable中的JCheckBox状态无法改变,也无法选中:下面是我的源代码,请热心的人帮助,自己查资料什么的弄了好久了,先谢了
public class TestSetRouterPane extends JPanel{
/**
* @param args
*/
JTable testTable = new JTable() ;
JPanel sPanel = new JPanel() ;
static List routList = new ArrayList() ;
public TestSetRouterPane() {
DBManageFactory factory = new RouteManagerFactory() ;
final IBeanManager manager = factory.getDBManager() ;
routList = manager.getAll() ;
testTable.setModel(new RouterTableModel(
)) ;
testTable.getTableHeader().setReorderingAllowed(false) ;
testTable.getTableHeader().setResizingAllowed(false) ;
//表头的第一列设置为JCheckBox
final TableColumn column = testTable.getTableHeader().getColumnModel().getColumn(0) ;
testTable.getTableHeader().setBorder(BorderFactory.createLineBorder(Color.gray)) ;
final HeaderCellRenderer check = new HeaderCellRenderer() ;
column.setHeaderRenderer(check) ;
//将JCheckBox设置为可选的
testTable.getTableHeader().addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e){
if(testTable.getColumnModel().getColumnIndexAtX(e.getX())==0){//如果点击的是第0列,即checkbox这一列
JCheckBox Checkbox = (JCheckBox)check;
boolean b = !check.isSelected();
check.setSelected(b);
testTable.getTableHeader().repaint();
}
}
});
//整个第一列的修饰
FirstColumnCellRender columnCheck = new FirstColumnCellRender() ;
testTable.getColumnModel().getColumn(0).setCellRenderer(columnCheck) ;
columnCheck.setHorizontalAlignment(JLabel.CENTER);
sPanel.setLayout(new BorderLayout()) ;
JScrollPane scrollPane = new JScrollPane(testTable) ;
sPanel.add(scrollPane, BorderLayout.CENTER) ;
add(sPanel, BorderLayout.CENTER) ;
}
class FirstColumnCellRender extends JCheckBox implements TableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
// TODO Auto-generated method stub
if(((RouteBean)(routList.get(row))).getGateway().equals("直连")){
return null ;
} else {
return this ;