开发者

Using a listrenderer and check boxes in lwuit

I've created the following custom list renderer for lwuit. The renderer extends a checkbox but for some reason, the select and unselect (check/uncheck) functions dont work. If i setselected() all the items come selected and cant be unselected. Heres the sample code;

class TaskListRenderer extends CheckBox implements ListCellRenderer {

        public TaskListRenderer() {
            super();
        }

        public Component getListCellRendererComponent(List list, Object o, int i, boolean bln) {

            Tasks task = (Tasks) o;
            try {
                img = Image.createImage("/three.png");
            } catch (IOException ex) {
                ex.printStackTrace();
            }
//            
            setIcon(开发者_如何学Pythonimg);
            setText(task.getPhoneID() + " " + task.getDate());

            Style style = new Style();//(0xff0000, 0x00ff00, null, byte(128));
            style.setBgColor(0xff0000);
            style.setFgColor(0x00ff00);
            setSelectedStyle(style);
            return this;
        }

        public Component getListFocusComponent(List list) {
            return this;
//            throw new UnsupportedOperationException("Not supported yet.");
        }
    }


Hi The line of code you are missing is

Tasks task = (Tasks) o;
setSelected(task.isSelect());

See a working version hope this helps.

On a side note I highly recommend you move the code below to out side of the getListCellRendererComponent because it gets called many many times when displaying a list and will slow down the performance of your app.

img = Image.createImage("/three.png");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜