List 怎么让指定行选中状态 或者 设置 某行的颜色(在线等待)!!!! List 怎么让指定行选中状态 或者 设置 某行的颜色(在线等待)!!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 实现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; } 关于继承的问题 有没有人做过日语分词? java读取C#写的2进制文件问题 写一个拼图游戏时候的问题 这个怎么通过实现TrafficLog类的hashcode和equals方法来做呢??? java新手问题 关于2维数组的查找, 时间复杂度 这些都是什么错误呀 请教高手applet如何用socket和servlet进行通讯?(最好有项细代码) JDBC问题 关于&与&&的详细区分? 帮忙解决jdk的问题
下面是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;
}