一个jtable外观的问题

class_song 2007-10-11 09:56:05
使用jtable创建了一个表格,并把表格中某些行设置了底色(用以区分特殊的数据),现在的问题是,当选择一个有底色的行时,该行底色变成了灰色,原来的底色看不见了,请问有什么解决方案。号小分不多,请见谅。
...全文
78 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
AppleYang2006 2007-10-12
  • 打赏
  • 举报
回复
if (isSelected) {
component.setForeground( Color );
component.setBackground(Color); //这里的颜色设定你选中时想要的颜色
} else {
component.setForeground();
component.setBackground(); // 这里的颜色设定你不选中时想要的颜色
}
setBackground()肯定是改变背景色,肯定是要改CellRenderer,具体的代码就要根据你的实际功能来写了
class_song 2007-10-12
  • 打赏
  • 举报
回复
通过setForeground()和setBackground()两个方法,是把表格中的字的颜色改变了。而实际的情况是当选择一行时,该行底色变成了灰色,把以前的底色给当住了,现在是要取消那种默认的选择行变灰色。。
AppleYang2006 2007-10-11
  • 打赏
  • 举报
回复
改写JTable的CellRenderer
public class TableViewRenderer extends DefaultTableCellRenderer {

private Color unselectedForeground;
private Color unselectedBackground;

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {

if (isSelected) {
component.setForeground( table.getSelectionForeground());
component.setBackground(table.getSelectionBackground());
} else {
component.setForeground((unselectedForeground != null) ? unselectedForeground
: table.getForeground());
component.setBackground((unselectedBackground != null) ? unselectedBackground
: table.getBackground());
}
setFont(table.getFont());
setValue(value);
return this;
}
}
}

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧