List 怎么让指定行选中状态 或者 设置 某行的颜色(在线等待)!!!!

热烈的红颜 2008-06-18 08:39:50
List 怎么让指定行选中状态 或者 设置 某行的颜色(在线等待)!!!!
...全文
138 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
热烈的红颜 2008-06-19
  • 打赏
  • 举报
回复
哦 谢谢了 我搞一下
zwgs1985 2008-06-19
  • 打赏
  • 举报
回复
实现ListCellRenderer,或者从DefaultListCellRenderer继承,在getListCellRendererComponent方法中可以设置某行的颜色
下面是DefaultListCellRenderer的源代码,修改一下就行,可以设置选中的颜色,也可以设置有焦点的border:

public Component getListCellRendererComponent(
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus)
{
setComponentOrientation(list.getComponentOrientation());

Color bg = null;
Color fg = null;

JList.DropLocation dropLocation = list.getDropLocation();
if (dropLocation != null
&& !dropLocation.isInsert()
&& dropLocation.getIndex() == index) {

bg = UIManager.getColor("List.dropCellBackground");
fg = UIManager.getColor("List.dropCellForeground");

isSelected = true;
}

if (isSelected) {
setBackground(bg == null ? list.getSelectionBackground() : bg);
setForeground(fg == null ? list.getSelectionForeground() : fg);
}
else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}

if (value instanceof Icon) {
setIcon((Icon)value);
setText("");
}
else {
setIcon(null);
setText((value == null) ? "" : value.toString());
}

setEnabled(list.isEnabled());
setFont(list.getFont());

Border border = null;
if (cellHasFocus) {
if (isSelected) {
border = UIManager.getBorder("List.focusSelectedCellHighlightBorder");
}
if (border == null) {
border = UIManager.getBorder("List.focusCellHighlightBorder");
}
} else {
border = getNoFocusBorder();
}
setBorder(border);

return this;
}

62,616

社区成员

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

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