LWUIT 上使用List实现表格

最近在做个类似的 lwuit 表格形式的。可是就是到这里卡住了。。

幸运的是 张国威 发表了这篇文章。

 

本文来自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,但是多很多方法和属性),所以两者的运行速度都差不多,至于用哪个,就看你喜欢了。

 

 

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

 


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

view plaincopy to clipboardprint?
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;  
        }  
    }  

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源代码如下:

view plaincopy to clipboardprint?
/* 
 * 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);  
        }  
    }  

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/hellogv/archive/2009/11/04/4767263.aspx

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值