我在JScrollPane(= spList)中有一个JList(= LstIncredients),其中包含对象的字符串表示形式(= Incredients)。我可以“添加”和“删除”这些Incredients,并且可以将其加载或保存到Harddisk。开始时,保存的列表(ArrayList)将以HD格式加载并正确显示在Jlist中。 问题:如果我删除了一些并添加了其他所有内容,一切正常,但是如果我在开始的地方多添加一个,则JList显示为完全空。如果我保存并重新启动程序,则只有这样,我添加的所有Incredients(包括)才能正确显示。
从ActionListener按钮中添加代码以从ListModel中添加和删除Incredient +代码:
public void actionPerformed(ActionEvent e) {
switch (e.getActionCommand()) {
case "add":
System.out.println("add"); //for debug only
Incredient i=getIncredientFromBoxes(); //Generates new Incredient from textboxes
Incredient.addIncredient(i); //adds an Incredient to Incredient-ArrayList
LstIncredients.repaint();
break;
case "del":
System.out.println("del");
Incredient iDel = getIncredientFromBoxes();
Incredient.delIncredient(iDel);
LstIncredients.repaint();
break; ...
ListModel<String> incredientsModel = new ListModel<String>() {
@Override
public int getSize() {
String pattern = txtName.getText(); // pattern is for filtering the Incredients-Array-List
System.out.println("ListModel.getSitze: "+Incredient.getList(pattern).size()); //for debugging. Incredient-Class works fine
return Incredient.getList(pattern).size(); //returns a ArrayList
}
@Override
public String getElementAt(int index) {
String pattern = txtName.getText(); // filter for shown incredients
System.out.println("getElement at: "+index); //for debugging
return Incredient.getList(pattern).get(index).toString();
}
如果“ public int getSize()”大于开头的大小,则甚至不会调用“ public String getElementAt(int index)”。
也许有人可以帮助我?