Java JList Model remove(final DefaultListModel list, final int[] indices)

Here you can find the source of remove(final DefaultListModel list, final int[] indices)

Description

Removes the given elements from the given list.

License

Open Source License

Parameter

Parameter Description
list The list from which to remove elements.
indices The index of elements to remove.

Declaration

public static void remove(final DefaultListModel<?> list, final int[] indices) 

Method Source Code

//package com.java2s;
/*/*from ww  w.jav  a  2 s  .  c om*/
 *    Geotoolkit.org - An Open Source Java GIS Toolkit
 *    http://www.geotoolkit.org
 *
 *    (C) 2001-2012, Open Source Geospatial Foundation (OSGeo)
 *    (C) 2009-2012, Geomatys
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation;
 *    version 2.1 of the License.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 */

import javax.swing.*;
import java.util.Arrays;

public class Main {
    /**
     * Removes the given elements from the given list. This method tries to use
     * {@link DefaultListModel#removeRange} when possible in order to group events
     * together.
     * <p>
     * <strong>Warning:</strong> This method override the given {@code indices} array.
     *
     * @param list    The list from which to remove elements.
     * @param indices The index of elements to remove.
     */
    public static void remove(final DefaultListModel<?> list, final int[] indices) {
        // We must iterate in reverse order, because the
        // index after the removed elements will change.
        int i = indices.length;
        if (i != 0) {
            Arrays.sort(indices);
            int upper = indices[--i];
            int lower = upper;
            while (i != 0) {
                int previous = indices[--i];
                if (previous != lower - 1) {
                    if (lower == upper) {
                        list.remove(lower);
                    } else {
                        list.removeRange(lower, upper);
                    }
                    upper = previous;
                }
                lower = previous;
            }
            if (lower == upper) {
                list.remove(lower);
            } else {
                list.removeRange(lower, upper);
            }
        }
    }
}

Related

  1. hasOnlyValue(final ListModel model, final Object value)
  2. indexesOf(DefaultListModel model, String sa[])
  3. indexOf(Object obj, ListModel model)
  4. listAsListModel(ArrayList list)
  5. listModelEquals(final ListModel lm, final ListModel lm2)
  6. removeAll(DefaultListModel m, List o)
  7. removeFromList(String path, DefaultListModel model, JList list)
  8. setListModel(ListModel model, JList list)
  9. toDefaultListModel(ArrayList list)