简体   繁体   English

将项添加到JList会删除所有其他项

[英]Adding an item to a JList remove all other items

I need some help about adding items to JList. 我需要一些关于向JList添加项目的帮助。 I work on some "library" kind of project. 我在一些“图书馆”类项目上工作。 And I need to add readers to already existing JList. 我需要将读者添加到现有的JList中。 But when I try to add it, JList just resets, removes all the readers and starts adding readers to a new blank JList. 但是当我尝试添加它时,JList只会重置,删除所有读者并开始将读者添加到新的空白JList中。 But I don't need it to make new list but add it to the already existing one. 但我不需要它来制作新列表,而是将其添加到现有列表中。

Here's my code: 这是我的代码:

listCtenaru = new JList(vector);
vector = new Vector<String>();

    FileInputStream fos = new FileInputStream("myjlist.bin");
    ObjectInputStream oss = new ObjectInputStream(fos);
    listCtenaru = (JList)oss.readObject();

    listScroll = new JScrollPane();
    listScroll.add(listCtenaru);

and action listener event 和动作监听器事件

public void actionPerformed(ActionEvent e) {
       String jmeno = pole1.getText();
       String prijmeni = pole2.getText();

       listCtenaru.setListData(vector);
       vector.addElement(jmeno +" "+ prijmeni);

       pole1.setText("");
       pole2.setText("");

       pole1.requestFocus();

It seems that your are creating a new model each time you add a reader and you set it on your JList. 每次添加阅读器并在JList上设置它时,您似乎都在创建新模型。

I think it will be better to use a ListModel which is more flexible and suited for your JList. 我认为使用更灵活且适合您的JList的ListModel会更好。 See the Java tutorials : http://docs.oracle.com/javase/tutorial/uiswing/components/list.html 请参阅Java教程: http//docs.oracle.com/javase/tutorial/uiswing/components/list.html

Here is the javadoc for JList.setListData(vector) : 这是JList.setListData(vector)的javadoc:

Constructs a read-only ListModel from an array of items, and calls setModel with this model. 从项目数组构造只读ListModel,并使用此模型调用setModel。 Attempts to pass a null value to this method results in undefined behavior and, most likely, exceptions. 尝试将null值传递给此方法会导致未定义的行为,并且很可能会导致异常。 The created model references the given array directly. 创建的模型直接引用给定的数组。 Attempts to modify the array after invoking this method results in undefined behavior. 尝试在调用此方法后修改数组会导致未定义的行为。

It stated that you must not modifies the vector after a call on setListData() and it's exactly what you have done here. 它声明在调用setListData()之后你不能修改向量,而这正是你在这里所做的。

To add and remove items in a list, you can use a ListModel : 要添加和删除列表中的项,可以使用ListModel

// To create:
DefaultListModel model = new DefaultListModel();
for(Object item : (Object[])ois.readObject()) {
   model.addElement(item);
}
JList list = new JList(model);

// To add:
model.addElement("New element");

// To save:
oos.writeObject(model.toArray());

Incidentally, it is generally a bad idea to serialize a whole Swing component (like a JList ). 顺便提一下,序列化整个Swing组件(如JList )通常是个坏主意。 Instead, you should just serialize the data in it. 相反,您应该只序列化其中的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM