is Jlist Override the List automatic? (bug)?

走远了吗. 提交于 2019-12-20 07:46:20

问题


I hope I will get help, I will ask as general question:

I am using a JList, and due to the JList not have a (value,text) (so I can display text and use the value in my code). Because of this leak I create List of object (myList), that work parallel with the JList. Every item I add to JList I add to myList, so the same index will contain the same info in the two objects (JList and mylist) I use the JList.getselectedindex() method to get the index and use it in myList to pup information...

The problem: is when I select value, the next value of the myList is overridden with the first value!!! Is this problem known?

    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!!!


回答1:


I am using a JList, and due to the JList not have a (value,text) (so I can display text and use the value in my code)

It's really hard to understand your problem, but I "suspect" for the quoted line that you have a missunderstanding between JList model and text displayed by the JList itself. I think that's why you have a separate List.

The model can contain any object you want and the JList can also display a text as you want regardless the object itself. This last task is done by a 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;
    }
}

Now you want your JList keep Person objects to work with them later. This part is easy just create a ListModel and add elements into it:

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

And your JList will show the items as you want:



来源:https://stackoverflow.com/questions/19732777/is-jlist-override-the-list-automatic-bug

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!