首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

在JTable中增添自定义的checkbox

2012-10-08 
在JTable中添加自定义的checkbox在JTable中加入复选框JCheckbox,直接加是加不上去的,因为它自动将JCheckbo

在JTable中添加自定义的checkbox

在JTable中加入复选框JCheckbox,直接加是加不上去的,因为它自动将JCheckbox转为String,只有通过JTable的的tableModel将columnClass设为Boolean就可以了。要设置选中与不选中,将该单元格的值设置为Boolean.true 或Boolean.false。

?

下面这段代码是为checkbox单独设置的渲染器,背景色为红色,选中时的背景色为绿色。

?

?

 class TestTableCellRenderer extends JCheckBox implements TableCellRenderer{public TestTableCellRenderer(){     setBackground(Color.red);     setForeground(Color.yellow);     setOpaque(ture);}  @Override  public Component getTableCellRendererComponent(JTable table,    Object value, boolean isSelected, boolean hasFocus, int row,    int column) {           if(isSelected){           setBackground(Color.green);    }    Boolean b = (Boolean) value;                 this.setSelected(b.booleanValue());                 return this;      }
?

?

热点排行