简体   繁体   中英

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 . 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. You don't implement those methods so I would guess you get an empty model.

Don't extend AbstractListModel. Instead extend the DefaultListModel and use the strorage provided by that class (so there is no need for the Vector).

Then just implement your addElements() and extractElements() method in the extended class. 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.

This code does not deal with duplicate elements like: {1,2,1} in the integer array argument.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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