Example usage for javax.swing JList revalidate

List of usage examples for javax.swing JList revalidate

Introduction

In this page you can find the example usage for javax.swing JList revalidate.

Prototype

public void revalidate() 

Source Link

Document

Supports deferred automatic layout.

Usage

From source file:util.ui.UiUtilities.java

/**
 * Moves Selected Items from one List to another
 *
 * @param fromList/*www .  j a va2s.  c  o  m*/
 *          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;
}