我写了一个JTable,其中第一列被render成了checkbox,但是当每次选中某一行的时候,只有checkbox这一列没有高亮显示。怎么做才能让一整行都高亮显示选中?
是用isSelected吗?怎么用?多谢!
是用isSelected吗?怎么用?多谢!
解决方案 »
- 急!!急!!!我的二级缓存EhCache为什么无效?
- sql server2000企业管理器关系图新建失败
- java开发的困惑...
- 用java的applet调用delphi封装第三方动态库函数的exe文件出错,急!!!!!分不够再加
- 请问如何把tomcat,struts开发的web应用用installanywhere的软件打包发布啊?
- 请问一下怎么样用JAVA播放声音呢
- 如何能看到java中string类的实现源代码啊?
- 什么时候使用监听器,什么时候使用动作?
- java教学数据出售
- 寻找JBuilder 6 Enterprise的Serial Number和License Key
- 问个JDBC中UPDATE RESULTSET的问题
- 一个简单的问题请高手请救
boolean isSelected, boolean hasFocus, int row, int column) {
if (isSelected) {
this.setForeground(table.getSelectionForeground());
this.setBackground(table.getSelectionBackground());
}
else {
this.setForeground(table.getForeground());
this.setBackground(table.getBackground());
}
.................
我已经设定了table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);但是当选中一整行的时候,仍然是那一列checkbox没有变颜色。
我没有说清楚,我不是想要checkbox选中前后颜色发生变化,而是想如果如果选中一整行,那列包含checkbox的颜色应该是高亮显示的。我会先看看sun的例子。
class CheckCellRenderer implements TableCellRenderer { /**
* Component that renders a null cell.
*/
JLabel nullCell; /**
* Component that renders a selected check box.
*/
JCheckBox selectedCheckBox; /**
* Component that renders a non-selected check box.
*/
JCheckBox nonSelectedCheckBox;
CheckCellRenderer(JTable table) { nullCell = new JLabel("--");
nullCell.setHorizontalAlignment(SwingConstants.CENTER); selectedCheckBox = new JCheckBox();
selectedCheckBox.setSelected(true);
selectedCheckBox.setHorizontalAlignment(SwingConstants.CENTER);
selectedCheckBox.setBackground(table.getBackground()); nonSelectedCheckBox = new JCheckBox();
nonSelectedCheckBox.setSelected(false);
nonSelectedCheckBox.setHorizontalAlignment(SwingConstants.CENTER);
nonSelectedCheckBox.setBackground(table.getBackground());
}
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row, int column) {
if (value instanceof Boolean) {
Boolean checked = (Boolean)value;
if (checked.booleanValue()) {
return selectedCheckBox;
}
else {
return nonSelectedCheckBox;
}
}
else {
return nullCell;
}
}
}因为已经设定了整个checkbox的背景色,而且占满了cell的空间,因此当选中一行的时候,checkbox那一列没有高亮显示。有什么办法解决吗?是不是只能监听ListSelectionEvent,然后据此来设定所有checkbox的背景色?
和我那个设置一起加上就可以实现。
isSelected表示当前这个单元格是否选中,而不是表示这个单元格的值。
可是我上面写的那个CheckCellRenderer 没有setForeground方法,所以会出错
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row, int column) {
if (value instanceof Boolean) {
Boolean checked = (Boolean)value;
if (checked.booleanValue()) {
if (isSelected) {
selectedCheckBox.setForeground(table.getSelectionForeground());
selectedCheckBox.setBackground(table.getSelectionBackground());
}
else {
selectedCheckBox.setForeground(table.getForeground());
selectedCheckBox.setBackground(table.getBackground());
}
return selectedCheckBox;
}
else {
if (isSelected) {
nonSelectedCheckBox.setForeground(table.getSelectionForeground());
nonSelectedCheckBox.setBackground(table.getSelectionBackground());
}
else {
nonSelectedCheckBox.setForeground(table.getForeground());
nonSelectedCheckBox.setBackground(table.getBackground());
}
return nonSelectedCheckBox;
}
}
else {
if (isSelected) {
nullCell.setForeground(table.getSelectionForeground());
nullCell.setBackground(table.getSelectionBackground());
}
else {
nullCell.setForeground(table.getForeground());
nullCell.setBackground(table.getBackground());
}
return nullCell;
}
}