向JList中添加JPanel,JPaen不可编辑的问题。

lewchiwi 2008-04-23 09:13:33
我在向JList中添加JPanel时,JPanel变得不可编辑了,是为什么啊,怎么解决掉???
我测试的时候,发现JPanel根本就没有办法监听到事件,即响应不了鼠标的点击事件。
...全文
265 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
dancingtiger 2008-04-29
  • 打赏
  • 举报
回复
dui
song_ice 2008-04-23
  • 打赏
  • 举报
回复
为什么要向jlist里面添加jpanel?如果真需要这么做,就需要重写Renderer,如:
class MyCellRenderer extends JLabel implements ListCellRenderer {
final static ImageIcon longIcon = new ImageIcon("long.gif");
final static ImageIcon shortIcon = new ImageIcon("short.gif");
public Component getListCellRendererComponent(
JList list,
Object value, // value to display
int index, // cell index
boolean isSelected, // is the cell selected
boolean cellHasFocus) // the list and the cell have the focus
{
String s = value.toString();
setText(s);
setIcon((s.length() > 10) ? longIcon : shortIcon);
if (isSelected) {
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
}
else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}
setEnabled(list.isEnabled());
setFont(list.getFont());
setOpaque(true);
return this;
}
}

String[] data = {"one", "two", "three", "four"};
JList dataList = new JList(data);
dataList.setCellRenderer(new MyCellRenderer());
lewchiwi 2008-04-23
  • 打赏
  • 举报
回复
有没有高手啊,帮帮忙

62,623

社区成员

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

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