1

我为 lwit 创建了以下自定义列表渲染器。渲染器扩展了一个复选框,但由于某种原因,选择和取消选择(选中/取消选中)功能不起作用。如果我setselected()所有的项目都被选中并且不能被取消选中。这是示例代码;

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(img);
            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.");
        }
    }
4

1 回答 1

3

您好您缺少的代码行是

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

查看工作版本希望这会有所帮助。

附带说明一下,我强烈建议您将下面的代码移到 getListCellRendererComponent 之外,因为它在显示列表时会被多次调用,并且会降低应用程序的性能。

img = Image.createImage("/three.png");
于 2011-03-24T07:49:45.997 回答