getTableCellRendererComponent 中isSelected疑问 我写了一个JTable,其中第一列被render成了checkbox,但是当每次选中某一行的时候,只有checkbox这一列没有高亮显示。怎么做才能让一整行都高亮显示选中? 是用isSelected吗?怎么用?多谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 isSelected: 目标单元格当前如果是选中的,则为true,反之为false,不高亮显示跟render成了checkbox应该没什么关系,把table的选择模式设为table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);看看,再不行就看一个这个吧http://community.csdn.net/Expert/topic/4131/4131260.xml?temp=.5156061 public Component getTableCellRendererComponent(JTable table, Object value, 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()); } ................. 多谢 fog628(无名) !我已经设定了table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);但是当选中一整行的时候,仍然是那一列checkbox没有变颜色。 Acylas(Acylas)多谢!我没有说清楚,我不是想要checkbox选中前后颜色发生变化,而是想如果如果选中一整行,那列包含checkbox的颜色应该是高亮显示的。我会先看看sun的例子。 因为table中保存checkbox的那一列有些值是空值,这时需要render成两个横线“--”,而不是checkbox。我的程序如下: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的背景色? table.setSelectionMode和我那个设置一起加上就可以实现。isSelected表示当前这个单元格是否选中,而不是表示这个单元格的值。 多谢Acylas回复!可是我上面写的那个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; } } 初学Java求大神指教 java多线程 定时任务Timer的问题 线程同步的问题 一个用程序操作系统浏览器的问题,请教高手? 请教 关于数组排序的问题 谁能帮忙解决这个DATE的问题,比较有挑战,急,谢谢! 求救!!!struts的datasource的问题,弄了一天了 关于邮件服务器的问题 有关基于JAVA的B/S模式下动态图象生成…… 新手紧急求教! 问个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;
}
}