Java Jlist是否自动覆盖列表?(臭虫)?
我希望能得到帮助,我将作为一般性问题提问: 我使用的是Java Jlist是否自动覆盖列表?(臭虫)?,java,swing,jlist,Java,Swing,Jlist,我希望能得到帮助,我将作为一般性问题提问: 我使用的是JList,由于JList没有(值,文本)(因此我可以显示文本并使用代码中的值)。由于这个漏洞,我创建了对象(myList)的列表,它与JList并行工作。我添加到JList的每个项目都添加到myList,因此相同的索引将在两个对象(JList和myList)中包含相同的信息 我使用JList.getselectedindex()方法获取索引,并在myList中使用它来获取信息 问题:是当我选择值时,myList的下一个值将被第一个值覆盖!!
JList
,由于JList
没有(值,文本)(因此我可以显示文本并使用代码中的值)。由于这个漏洞,我创建了对象(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
中有两个项目时,如果我选择了任何项目,另一个项目将被覆盖
我使用的是JList
,由于JList
没有(值,文本)(因此我
可以显示文本并使用“我的代码”中的值)
很难理解您的问题,但我“怀疑”您对JList
模型和JList
本身显示的文本之间的错误理解。我想这就是为什么你有一个单独的列表
模型可以包含您想要的任何对象,JList
也可以根据您的需要显示文本,而不考虑对象本身。最后一项任务由一名工程师完成。看看
例如,您可以使用以下类:
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
对象,以便以后使用它们。这部分很简单,只需创建一个列表模型
,并向其中添加元素:
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"));
但您希望显示每个人的姓名。您可以实现自己的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
将显示您想要的项目:
为什么不创建一个保存值的对象并将其传递给JList模型?你可以重写它的toString方法,这样它会显示你想要的文本?+1代表idea,但不确定OPs问题是关于THX dic19的,我将根据你的好例子解释我的问题:我想在列表中显示名字,并在代码中使用姓氏(不显示它)。。。i、 e我想显示John,并创建一个名为Lennon.txt的文件……好吧,那么,我并没有那么迷路:P按照示例:1)在您的ListCellRenderer
中创建:setText(person.getName())
。2) 假设您有一个类似于saveFile(字符串文件名)
的方法,您可以这样做:saveFile(((Person)list.getSelectedValue()).getLastName())
。由于列表
的模型包含个人
对象,因此您可以按此方式强制转换所选值,并根据需要使用它@埃斯迈尔卡什