// Display an icon and a string for each object in the list.
class MyCellRenderer extends JLabel implements ListCellRenderer {
final static ImageIcon longIcon = new ImageIcon("long.gif");
final static ImageIcon shortIcon = new ImageIcon("short.gif"); // This is the only method defined by ListCellRenderer.
// We just reconfigure the JLabel each time we're called. 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());document里面的
class MyCellRenderer extends JLabel implements ListCellRenderer {
final static ImageIcon longIcon = new ImageIcon("long.gif");
final static ImageIcon shortIcon = new ImageIcon("short.gif"); // This is the only method defined by ListCellRenderer.
// We just reconfigure the JLabel each time we're called. 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());document里面的
解决方案 »
- 数组排序(含有字母)
- setSize 和 setPreferredSize 有什么区别
- 求 怎么搞定UML? 马上要开学了 大家帮帮忙啊
- java网络问题,很简单(60分立结)
- xalan2.6.2在jdk1.4中运行时问题,已经卡到这里三天了,求救,另开贴加分
- 可以运行成功,只是没有出现警告对话框!!部分程序如下::
- 急,用socket,接收到的数据不对?
- 需要查找Java文件注释代码行的正则表达式
- 关于Java编码规范
- 请问application (or applet)如何实现对applet 小程序的调用??
- 可用分一天只固定加10分,有问题都不敢发贴子了
- Jcreator Pro的一个小问题
String s = "Hello";
v.addItem(s);
JList l = new JList(v);
...
String s = (String) v.item(0);// 可能不是item(),我忘了