包含在列表中的可单击组件的GenericListCellRenderer不起作用

时间:2016-03-21 13:12:52

标签: java codenameone

我正在尝试在GenericListCellRenderer上设置开关组件,但它会因内部应用程序错误 - NullPointer异常而抛出错误。

我试过的代码如下。

Form hi = new Form("Hi World");
        hi.setLayout(new BorderLayout());

        com.codename1.ui.List list = new com.codename1.ui.List(createGenericListCellRendererModelData());
        list.setRenderer(new GenericListCellRenderer(createGenericRendererContainer(), createGenericRendererContainer()));
        Button b = ((GenericListCellRenderer)list.getRenderer()).extractLastClickedComponent();
        System.out.println("button >> " +b);
        if(b != null) {
            System.out.println("button");
            // handle button click event and return
            Dialog.show("test", "test", "OK", null);
            return;
        }

        list.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                // TODO Auto-generated method stub
                Dialog.show("hello", "test", "OK", null);
            }
        });

        hi.add(BorderLayout.CENTER,list);
        hi.show();

private Container createGenericRendererContainer() {
        Label name = new Label();
        name.setFocusable(true);
        name.setName("Name");
        Label surname = new Label();
        surname.setFocusable(true);
        surname.setName("Surname");

        OnOffSwitch selected = new OnOffSwitch();
        selected.setName("Selected");
        selected.setFocusable(true);
        selected.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                // TODO Auto-generated method stub
                Dialog.show("switch", "switch", "OK", null);
            }
        });

        Container c = new Container(new BorderLayout());
        c.setUIID("ListRenderer");
        Container cc = new Container(new GridLayout(1, 4)) ;
        cc.add(new Label("test1")).add(new Label("test2")).add(new Label("test3")).add(new Label("test4"));

        c.add(BorderLayout.WEST,selected);
        c.add(BorderLayout.CENTER, cc);


        return c;
    }

    private Object[] createGenericListCellRendererModelData() {
        Map<String,Object>[] data = new HashMap[5];
        data[0] = new HashMap<String, Object>();
        data[0].put("Name", "Shai");
        data[0].put("Surname", "Almog");
        data[0].put("Selected", Boolean.TRUE);
        data[1] = new HashMap<String, Object>();;
        data[1].put("Name", "Chen");
        data[1].put("Surname", "Fishbein");
        data[1].put("Selected", Boolean.TRUE);
        data[2] = new HashMap<String, Object>();
        data[2].put("Name", "Ofir");
        data[2].put("Surname", "Leitner");
        data[3] = new HashMap<String, Object>();
        data[3].put("Name", "Yaniv");
        data[3].put("Surname", "Vakarat");
        data[4] = new HashMap<String, Object>();
        data[4].put("Name", "Meirav");
        data[4].put("Surname", "Nachmanovitch");
        return data;
    }

1 个答案:

答案 0 :(得分:0)

由于列表使用橡皮图章方法,因此您所指的组件不再存在。您只需收听List个事件,因为点击始终位于List上。

解决方案是使用extractLastClickedComponent()GenericListCellRenderer获取最后点击的组件。

E.g:

    list.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent evt) {
            GenericListCellRenderer gn = (GenericListCellRenderer)list.getRenderer();
            Component lastCmp = gn.extractLastClickedComponent();
            if(lastCmp != null) {
               Dialog.show("hello", "Clicked: " + lastCmp.getName(), "OK", null);
            } else {
               Dialog.show("hello", "test", "OK", null);
            }
        }
    });