Java JList Select moveSelectedItems(JList list, int nrRows)

Here you can find the source of moveSelectedItems(JList list, int nrRows)

Description

Move selected Items in the JList

License

Open Source License

Parameter

Parameter Description
list Move Items in this List
nrRows Move Items nrRows up/down

Declaration

public static void moveSelectedItems(JList list, int nrRows) 

Method Source Code

//package com.java2s;
/*//w  w  w  .  j  a v  a 2s . c o m
 * TV-Browser
 * Copyright (C) 04-2003 Martin Oberhauser (martin_oat@yahoo.de)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * CVS information:
 *  $RCSfile$
 *   $Source$
 *     $Date: 2010-11-14 17:44:44 +0100 (Sun, 14 Nov 2010) $
 *   $Author: bananeweizen $
 * $Revision: 6823 $
 */

import javax.swing.DefaultListModel;

import javax.swing.JList;

public class Main {
    /**
     * Moves Selected Items from one List to another
     *
     * @param fromList
     *          Move from this List
     * @param toList
     *          Move into this List
     * @return Moved Elements
     */
    public static Object[] moveSelectedItems(JList fromList, JList toList) {
        DefaultListModel fromModel = (DefaultListModel) fromList.getModel();
        DefaultListModel toModel = (DefaultListModel) toList.getModel();

        // get the selection
        int[] selection = fromList.getSelectedIndices();

        if (selection.length == 0) {
            return new Object[] {};
        }

        Object[] objects = new Object[selection.length];
        for (int i = 0; i < selection.length; i++) {
            objects[i] = fromModel.getElementAt(selection[i]);
        }

        // get the target insertion position
        int targetPos = toList.getMaxSelectionIndex();
        if (targetPos == -1) {
            targetPos = toModel.getSize();
        } else {
            targetPos++;
        }

        // suppress updates on both lists
        if (selection.length >= 5) {
            fromList.setModel(new DefaultListModel());
            toList.setModel(new DefaultListModel());
        }

        // move the elements
        for (int i = selection.length - 1; i >= 0; i--) {
            Object value = fromModel.remove(selection[i]);
            toModel.add(targetPos, value);
        }

        if (selection.length >= 5) {
            fromList.setModel(fromModel);
            toList.setModel(toModel);
        }

        // change selection of the fromList
        if (fromModel.getSize() > 0) {
            int newSelection = selection[0];
            if (newSelection >= fromModel.getSize()) {
                newSelection = fromModel.getSize() - 1;
            }
            fromList.setSelectedIndex(newSelection);
        }

        if (selection.length >= 5) {
            fromList.repaint();
            fromList.revalidate();
            toList.repaint();
            toList.revalidate();
        }

        // change selection of the toList
        toList.setSelectionInterval(targetPos, targetPos + selection.length - 1);

        // ensure the selection is visible
        toList.ensureIndexIsVisible(toList.getMaxSelectionIndex());
        toList.ensureIndexIsVisible(toList.getMinSelectionIndex());

        return objects;
    }

    /**
     * Moves Selected Items from one List to another
     *
     * @param fromList
     *          Move from this List
     * @param toList
     *          Move into this List
     * @param row
     *          The target row where to insert
     * @return Moved Elements
     */
    public static Object[] moveSelectedItems(JList fromList, JList toList, int row) {
        DefaultListModel fromModel = (DefaultListModel) fromList.getModel();
        DefaultListModel toModel = (DefaultListModel) toList.getModel();

        // get the selection
        int[] selection = fromList.getSelectedIndices();

        if (selection.length == 0) {
            return new Object[] {};
        }

        Object[] objects = new Object[selection.length];
        for (int i = 0; i < selection.length; i++) {
            objects[i] = fromModel.getElementAt(selection[i]);
        }

        // move the elements
        for (int i = selection.length - 1; i >= 0; i--) {
            Object value = fromModel.remove(selection[i]);
            toModel.insertElementAt(value, row);
        }

        // change selection of the fromList
        if (fromModel.getSize() > 0) {
            int newSelection = selection[0];
            if (newSelection >= fromModel.getSize()) {
                newSelection = fromModel.getSize() - 1;
            }
            // fromList.setSelectedIndex(-1);
        }

        // change selection of the toList
        toList.setSelectionInterval(row, row + selection.length - 1);

        // ensure the selection is visible
        toList.ensureIndexIsVisible(toList.getMaxSelectionIndex());
        toList.ensureIndexIsVisible(toList.getMinSelectionIndex());

        return objects;
    }

    /**
     * Move selected Items in the JList
     *
     * @param list
     *          Move Items in this List
     * @param row
     *          The target row where to insert
     * @param sort
     *          Dummy parameter, does nothing
     */
    public static void moveSelectedItems(JList list, int row, boolean sort) {
        DefaultListModel model = (DefaultListModel) list.getModel();

        // get the selection
        int[] selection = list.getSelectedIndices();
        if (selection.length == 0) {
            return;
        }

        boolean lower = false;
        // Remove the selected items
        Object[] items = new Object[selection.length];
        for (int i = selection.length - 1; i >= 0; i--) {
            if (selection[i] < row && !lower) {
                row = row - i - 1;
                lower = true;
            }
            items[i] = model.remove(selection[i]);
        }

        for (int i = items.length - 1; i >= 0; i--) {
            model.insertElementAt(items[i], row);
        }

        // change selection of the toList
        list.setSelectionInterval(row, row + selection.length - 1);

        // ensure the selection is visible
        list.ensureIndexIsVisible(list.getMaxSelectionIndex());
        list.ensureIndexIsVisible(list.getMinSelectionIndex());
    }

    /**
     * Move selected Items in the JList
     *
     * @param list
     *          Move Items in this List
     * @param nrRows
     *          Move Items nrRows up/down
     */
    public static void moveSelectedItems(JList list, int nrRows) {
        DefaultListModel model = (DefaultListModel) list.getModel();

        // get the selection
        int[] selection = list.getSelectedIndices();
        if (selection.length == 0) {
            return;
        }

        // Remove the selected items
        Object[] items = new Object[selection.length];
        for (int i = selection.length - 1; i >= 0; i--) {
            items[i] = model.remove(selection[i]);
        }

        // insert the elements at the target position
        int targetPos = selection[0] + nrRows;
        targetPos = Math.max(targetPos, 0);
        targetPos = Math.min(targetPos, model.getSize());
        for (int i = 0; i < items.length; i++) {
            model.add(targetPos + i, items[i]);
        }

        // change selection of the toList
        list.setSelectionInterval(targetPos, targetPos + selection.length - 1);

        // ensure the selection is visible
        list.ensureIndexIsVisible(list.getMaxSelectionIndex());
        list.ensureIndexIsVisible(list.getMinSelectionIndex());
    }
}

Related

  1. getSelectedMultipleList(JList anJList)
  2. getSelectedRows(ListSelectionModel listSelectionModel)
  3. initParamList(final JList paramList, final String[] availNames, final String[] defaultSelection)
  4. JListRemoveSelectedObject(javax.swing.JList list)
  5. linkEnabledToSelected(final JComponent component, final ListSelectionModel model)
  6. moveSourceSelection(JList sourceList, boolean moveDown, boolean byPage)
  7. removeListSelectionListeners(final JList comp)
  8. removeSelectedListItems(JList sourceList)
  9. removeSelectionFromList(JList fileList)