是Jlist自动覆盖列表? (错误)?

时间:2013-11-01 18:13:12

标签: java swing jlist

我希望我会得到帮助,我会问一般问题:

我使用的是JList,由于JList没有(值,文本)(因此我可以显示文本并使用代码中的值)。由于这种泄漏,我创建了List对象(myList),它与JList并行工作。我添加到JList的每个项目都添加到myList,因此相同的索引将在两个对象(JList和mylist)中包含相同的信息 我使用JList.getselectedindex()方法获取索引并在myList中将其用于小狗信息......

问题:是我选择值时,myList的下一个值会被第一个值覆盖!!! 这个问题是否已知?

    mod_mp = new ModelMAPPING();   objects cotain values that ot exist in  jList                                                             

    msgF.setTo(incom.userID);/////// set parter!
    if(isExCon==-1) {
        // not exist                                           
        mod_mp.to = incom.userID; // incom is object that incom from another program
        mod_mp.SetCovFile(incom.userID+".html");
        mod_mp.ConvName = incom.getBody();

        boolean added= model_list.add(mod_mp);   // add to mylist
        if(added) System.out.println(mod_mp._Hfile + " added");
        model.addElement(mod_mp.ConvName);// add to Jlist by model

        HestoryFile(Htmlhead+tohis,mod_mp._Hfile);//create _Hfile and write to it:"tohis" string.

    } else { //exist@
        // note isExcon return the index if exist else -1
        model_list.get(isExCon).ConvName=incom.getBody();
        mod_mp.SetCovFile(model_list.get(isExCon)._Hfile);
        HestoryFile(tohis, model_list.get(isExCon)._Hfile);
    }//end else

如果文件存在,我只需更新JList中的新文本并设置当前文件

JList的选择是:

msgF.setTo (model_list.get(jList2.getSelectedIndex()).to); // set that we will send To...
mod_mp.SetCovFile(model_list.get(jList2.getSelectedIndex())._Hfile);//set the file

jLabel5.setText( bringFromFile(mod_mp._Hfile));//tell the label to read that file

它工作正常,但如果我在JList中有两个项目,如果我选择任何一个,则另一个被覆盖!

1 个答案:

答案 0 :(得分:2)

  

我使用JList,因为JList没有(值,文字)(所以我   可以显示文本并使用我的代码中的值)

理解您的问题真的很难,但我“怀疑”引用的一行是JList模型与JList本身显示的文字之间存在误解。我认为这就是为什么你有一个单独的List

模型可以包含您想要的任何对象,JList也可以根据需要显示文本,无论对象本身如何。最后一项任务由ListCellRenderer完成。看看Writing a Custom Cell Renderer

例如,您可以拥有此课程:

class Person {    
    String lastName;
    String name;

    public Person(String lastName, String name){
        this.lastName = lastName;
        this.name = name;
    }

    public String getLastName(){
        return this.lastName;
    }

    public String getName(){
        return this.name;
    }
}

现在您希望JList保留Person个对象以后再使用它们。这部分很简单,只需创建一个ListModel并在其中添加元素:

DefaultListModel model = new DefaultListModel();
model.addElement(new Person("Lennon","John"));
model.addElement(new Person("Harrison","George"));
model.addElement(new Person("McCartney","Paul"));
model.addElement(new Person("Starr","Ringo"));

但您想显示每个Person的名称和姓氏。那么你可以实现自己的ListCellRenderer来做到这一点:

JList list = new JList(model);
list.setCellRenderer(new DefaultListCellRenderer(){
    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        if(value instanceof Person){
            Person person = (Person)value;
            setText(person.getName() + " " + person.getLastName());
        }
        return this;
    }
});

您的JList会根据需要显示相关项目:

enter image description here