一尘不染

Jlist Override the List是自动的吗?(bug)?

java

希望我能得到帮助,我会问一个一般性的问题:

我正在使用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

Here if file exists I just update the new text in the JList and set the
current file

The select of JList is:

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

It works fine, but when I have two items in JList if I select any, the other
is overridden!!!


阅读 172

收藏
2020-12-03

共1个答案

一尘不染

我正在使用JList,并且由于JList没有(值,文本)(所以我
可以显示文本并在代码中使用值)

很难理解您的问题,但是我“怀疑”
这句话,因为您对JList模型和
其JList本身显示的文本有误解。我认为这就是为什么您需要单独的
List。

该模型可以包含所需的任何对象,并且JList还可以
根据需要显示文本,而不管对象本身如何。最后一项任务由
ListCellRenderer.
Take a look to Writing a Custom Cell
Renderer

For instance you can have this class:

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

But you want to display the name and last name of each Person. Well you can
implement your own ListCellRenderer to do this:

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;
    }
});
2020-12-03