【发布时间】:2016-02-16 02:59:43
【问题描述】:
所以我创建了这个自定义 ListModel,但是当我调用 addElements 和 extractElements 时,它并没有更新相关 JList 上的可见内容。我怀疑我没有正确使用各种“开火”方法,但我自己看不到任何问题。感谢您的帮助,谢谢
public class PartsList extends AbstractListModel {
private Vector<String> parts;
public PartsList() {
parts = new Vector<String>();
}
/**
* Adds an array of Strings to this list
*
* @param toAdd
* The array of Strings to add
*/
public void addElements(String[] toAdd) {
for (String item : toAdd) {
parts.addElement(item);
}
fireContentsChanged(this, 0, parts.size() - toAdd.length);
fireIntervalAdded(this, parts.size() - toAdd.length, parts.size());
}
/**
* Takes a list of indices and returns a String array of the
* items at those indices while removing them
*
* @param toGet
* The (sorted) int array containing the indices
* @return String[] The items saved at the given indices
*/
public String[] extractElements(int[] toGet) {
String[] items = new String[toGet.length];
int i = 0;
for (int item : toGet) {
items[i] = parts.remove(item - i); // The -i cancels out the fact an item was removed in previous iterations
System.out.println(item - i);
i++;
}
fireIntervalRemoved(this, parts.size(), parts.size() + i);
fireContentsChanged(this, 0, parts.size());
return items;
}
}
更新:已解决
这是一些我忘记在不同的类中清理的旧代码,它会干扰这个,但不会以某种方式干扰其他任何东西。 我只想删除这个问题,但我不能。
【问题讨论】: