日期:2014-05-20 浏览次数:20723 次
package com.onest.xmair.test; import com.sun.lwuit.Component; import com.sun.lwuit.Container; import com.sun.lwuit.Label; import com.sun.lwuit.List; import com.sun.lwuit.events.ActionListener; import com.sun.lwuit.layouts.BorderLayout; import com.sun.lwuit.layouts.BoxLayout; import com.sun.lwuit.list.ListCellRenderer; import com.sun.lwuit.plaf.Border; public class Cls_CellList { static public List createList(CellList[] celllist,int stringWidth,ActionListener lAListner) { List list = new List(celllist); list.getStyle().setBgTransparency(0); //开始显示List list.setListCellRenderer(new CellRenderer(celllist[0].getColumm().length, stringWidth)); //添加消息处理 list.addActionListener(lAListner); return list; } static public class CellRenderer extends Container implements ListCellRenderer { //初始化显示Columm每个字段的Label private Label[] lbColumm ; private Label focus = new Label(""); public CellRenderer(int size,final int stringWidth) { lbColumm = new Label[size]; setLayout(new BorderLayout()); Container cnt = new Container(new BoxLayout(BoxLayout.X_AXIS)); for(int i=0; i < lbColumm.length; i ++)//定义显示Columm每个字段的Label { lbColumm[i] = new Label(){ //很多人都问怎么setWidth平时没什么用,这是因为setWidth必须在重载的时候使用,LWUIT奇怪的地方之一! public void setWidth(int arg0) { super.setWidth(stringWidth); } }; //设置为最亮的颜色 lbColumm[i].getStyle().setFgColor(getStyle().getFgSelectionColor()); //设置背景为透明,不然会遮挡focus影响外观 lbColumm[i].getStyle().setBgTransparency(0); // 新增代码 lbColumm[i].setFocusable(true); cnt.addComponent(lbColumm[i]); } // 新增代码 cnt.setFocusable(true); //cnt.setScrollableX(true); //cnt.setScrollableY(true); addComponent(BorderLayout.CENTER, cnt); focus.getStyle().setBgTransparency(255); focus.getStyle().setBorder(Border.createRoundBorder(10, 10)); } public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected) { CellList cellList = (CellList) value; for(int i=0;i<lbColumm.length;i++) { lbColumm[i].setText(cellList.getColumm()[i]); } return this; } public Component getListFocusComponent(List list) { //Columm栏不具备选中事件 if(list.getSelectedIndex()>0) { return focus; } return null; } } static public class CellList { //保存该列的所有字段 private String[] lstColumm; public CellList(String[] columm) { //复制数组 lstColumm=new String[columm.length]; System.arraycopy(columm, 0, lstColumm, 0, lstColumm.length); } //返回该列 public String[] getColumm(){ return lstColumm; } } }