Jlist Override the List是自动的吗? (漏洞)? [英] is Jlist Override the List automatic? (bug)?

查看:70
本文介绍了Jlist Override the List是自动的吗? (漏洞)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

问题:当我选择值时,myList的下一个值将被第一个值覆盖!!! 这个问题知道吗?

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

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

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

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中有两个项目(如果我选择了任何一个),则另一个将被覆盖!!!

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

推荐答案

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

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)

真的很难理解您的问题,但是我怀疑"引号行,您在JList模型和JList本身显示的文本之间存在误解.我认为这就是为什么您有单独的List.

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.

模型可以包含所需的任何对象,并且JList还可以根据需要显示文本,而不管对象本身如何.最后一项任务由 ListCellRenderer 完成.看看编写自定义单元格渲染器

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

现在,您希望您的JList保留Person对象,以便以后使用它们.只需创建ListModel并向其中添加元素,这部分就很容易了:

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

但是您想显示每个Person的名称和姓氏.好吧,您可以实现自己的ListCellRenderer来做到这一点:

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

您的JList将根据需要显示项目:

And your JList will show the items as you want:

这篇关于Jlist Override the List是自动的吗? (漏洞)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆