-1

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

dic19
  • 17,821
  • 6
  • 40
  • 69
EsmaeelQash
  • 488
  • 2
  • 6
  • 20
  • Why don't you create and object that holds the values and pass it to your JList model? And you can override it's toString method so it'll display the text you want? – regulus Nov 01 '13 at 18:18

1 Answers1

2

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:

enter image description here

dic19
  • 17,821
  • 6
  • 40
  • 69
  • 1
    +1 for idea, but not sure that OPs question is about – mKorbel Nov 01 '13 at 19:51
  • thx dic19, I will explain my problem according to your great example: I want to Display the first name i the list, and use the last name in my code(without displaying it)... i.e I want to display John, and create file Called Lennon.txt... – EsmaeelQash Nov 01 '13 at 19:58
  • Alright then, I wasn't that lost :P Following the example: 1) In your `ListCellRenderer` make this: `setText(person.getName())`. 2) Assuming you have a method like `saveFile(String fileName)` you can do this: `saveFile(((Person)list.getSelectedValue()).getLastName())`. As `list`'s model contains `Person` objects you can cast the selected value as such and work with it as needed. @EsmaeelQash – dic19 Nov 01 '13 at 20:10