简体   繁体   English

自定义列表模型不更新JList

[英]Custom list model not updating JList

So I made this custom ListModel, but it's not updating what's visible on the associated JList when I call addElements and extractElements . 因此,我制作了这个自定义ListModel,但是当我调用addElementsextractElements时,它并没有更新在关联的JList上可见的extractElements I suspect I'm not using the various "fire" methods correctly, but I can't see any problems myself. 我怀疑我没有正确使用各种“射击”方法,但是我自己看不到任何问题。 Any help is appreciated, thank you 任何帮助表示赞赏,谢谢

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

Update: Solved 更新:已解决

The was some old code I forgot to clean up in a different class that was interfering with this but not anything else somehow. 这是我忘记在另一个类中清除的一些旧代码,它干扰了此代码,但以某种方式没有其他影响。 I would just delete this question, but I can't. 我只想删除这个问题,但是不能。

The JList will use the getSize() and getElementAt() methods to determine how many elements to display. JList将使用getSize()getElementAt()方法来确定要显示多少个元素。 You don't implement those methods so I would guess you get an empty model. 您没有实现这些方法,所以我猜您会得到一个空模型。

Don't extend AbstractListModel. 不要扩展AbstractListModel。 Instead extend the DefaultListModel and use the strorage provided by that class (so there is no need for the Vector). 而是扩展DefaultListModel并使用该类提供的存储空间(因此不需要Vector)。

Then just implement your addElements() and extractElements() method in the extended class. 然后,只需在扩展类中实现addElements()和extractElements()方法即可。 Your looping code will then just use the methods provided by the DefaultListModel class, like addElement(...) and remove(...) inside the loop to add/remove elements and those methods will fire the proper events. 然后,您的循环代码将只使用DefaultListModel类提供的方法,例如循环内的addElement(...)remove(...)来添加/删除元素,这些方法将触发适当的事件。

This code does not deal with duplicate elements like: {1,2,1} in the integer array argument. 此代码不处理整数数组参数中的重复元素,例如:{1,2,1}。

To deal with this you may want to use a set. 为了解决这个问题,您可能需要使用一组。

class PartsList extends AbstractListModel<String> {

private final Vector<String> parts;

public PartsList() {
    parts = new Vector<>();
}

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

/**
 * 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) {
    Arrays.sort(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
        i++;
    }
    fireContentsChanged(this, 0, parts.size());
    return items;
}

@Override
public int getSize() {
    return parts.size();
}

@Override
public String getElementAt(int index) {
        return parts.get(index);
}
}

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

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