LWUIT 上使用List实现表格

晨曦之光 发布于 2012/03/14 18:57
阅读 69
收藏 0

本文来自http://blog.csdn.net/hellogv/ ,引用必须注明出处!


      最近越来越多人用LWUIT,问得问题也越来越多了。最近超级多人问的是,如何使用List实现表格。以前我用Grid实现了表格(http://blog.csdn.net/hellogv/archive/2009/01/12/3759984.aspx ),现在就来说说如何使用List是一个表格。

      先来对比一下Grid表格与List表格:


Grid表格                                List表格

gird    list


Grid表格与List表格所消耗的资源差不多,List所使用的结构和控件(都是Label)略多,但是Grid使用的是Button(衍生于Label,但是多很多方法和属性),所以两者的运行速度都差不多,至于用哪个,就看你喜欢了。


本文的源代码可以到这里下载:http://download.csdn.net/source/1792549


本文关键的两个文件是:Cls_CellList. java 和ListDemo.java, Cls_CellList. java 封装了本文所用的List,而 ListDemo.java则是演示使用,代码简单易明,一般人也能看懂,呵呵呵。


Cls_CellList. java主要使用了ListCellRenderer这个东西,源代码如下:

package com.sun.lwuit.uidemo;
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());//设置为最亮的颜色
                lbColumm[i].getStyle().setBgTransparency(0);//设置背景为透明,不然会遮挡focus影响外观
                cnt.addComponent(lbColumm[i]);
            }
            
            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;
        }
    }
}


ListDemo.java源代码如下:

/*
 * Copyright ?2008 Sun Microsystems, Inc. All rights reserved.
 * Use is subject to license terms.
 *
 */
package com.sun.lwuit.uidemo;
import com.sun.lwuit.Command;
import com.sun.lwuit.Font;
import com.sun.lwuit.Form;
import com.sun.lwuit.List;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BorderLayout;
public class ListDemo implements ActionListener  {
    public Form form = new Form("ListDemo");
    private  Command backCommand = new Command("Back", 1);
    private String[][] myList = {
        {"  标题1  ","  标题2  ","  标题3  "," 标题4 "},//表格每个字段的显示长度由标题栏的最长一项决定
        {"aaaaa","bbbbb","ccccc","ddddd"},
        {"11111","222222","33333","444444"},
        {"aaaaa","bbbbb","ccccc","ddddd"},
        {"11111","222222","33333","444444"},
        {"aaaaa","bbbbb","ccccc","ddddd"},
        {"11111","222222","33333","444444"},
        {"aaaaa","bbbbb","ccccc","ddddd"},
        {"11111","222222","33333","444444"}
    };
    ListDemo(){
        form.setLayout(new BorderLayout());
        form.addCommand(backCommand);
        form.setScrollable(true);
        //定义列表的列数
        Cls_CellList.CellList[] cellArray = new Cls_CellList.CellList[myList.length];
        for (int i = 0; i < cellArray.length; i++) {
            cellArray[i] = new Cls_CellList.CellList(myList[i]);
        }
        
        //取得Columm的标题栏中长度最大的一项
        int stringWidth=0;
        for(int i=0;i<myList[0].length;i++)
        {
            int size=Font.getDefaultFont().stringWidth(myList[0][i])+8;//给最长的字段留点空余
            if(stringWidth<size)
                stringWidth=size;
        }
        //构造list并且显示
        form.addComponent(BorderLayout.CENTER,Cls_CellList.createList(cellArray,stringWidth,new ListActionListener()));
        form.setCommandListener(this);
    }
    public void actionPerformed(ActionEvent arg0) {  
        Command command=arg0.getCommand();
        if(command==backCommand)
            UIDemoMIDlet.backToMainMenu();
    }
    //按下列表触发的事件
    private class ListActionListener implements ActionListener {
        public void actionPerformed(ActionEvent evt) {
            int selectIndex=((List)evt.getSource()).getSelectedIndex();
            String str="";
            for(int i=0;i<myList[selectIndex].length;i++)
                str=str+myList[selectIndex][i]+"_";
            form.setTitle(str);
        }
    }
}



原文链接:http://blog.csdn.net/hellogv/article/details/4767263
加载中
返回顶部
顶部