gpt4 book ai didi

java - 列表中包含可点击组件的通用 ListCellRenderer 不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 12:47:54 25 4
gpt4 key购买 nike

我试图在 Generic ListCellRenderer 上设置开关组件,但它会抛出内部应用程序错误 - 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;
}

最佳答案

由于列表使用橡皮图章方法,您所引用的组件不再存在。您只需听 List自点击以来的事件将始终位于List上.

解决办法是从 GenericListCellRenderer 中获取最后点击的组件使用extractLastClickedComponent()

例如:

    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);
}
}
});

关于java - 列表中包含可点击组件的通用 ListCellRenderer 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36132022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com