java jlist多列_如果实现JList多列

尽管JList本身不支持多列显示,但可以通过自定义CellRenderer实现类似效果。本文介绍了一种方法,通过MyCellRenderer类调整JList的显示样式,使其看似多列。然而,实际应用中,使用JTable可能更为合适。
摘要由CSDN通过智能技术生成

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值