JList不能显示多个Column.
每个JList都有一个相应的ListModel控制其内容,它只能存放以array或vector组织起来的对象集合:
String[] data = { "one ", "two ", "three ", "four "};
JList dataList = new JList(data);
但也有人通过对JList的CellRenderer进行重新定义,可以使JList的每行元素显示所需的各种效果如下面代码,但是虽然得到各种显示效果,但其实质都没有变的。应该用Jtable更恰当。做一个东西应该选择最方便最有效的方法去做。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());
此代码出处:http://topic.csdn.net/t/20050807/18/4194280.html
2011年1月06日 14:46