Example usage for javax.swing ListModel getElementAt

List of usage examples for javax.swing ListModel getElementAt

Introduction

In this page you can find the example usage for javax.swing ListModel getElementAt.

Prototype

E getElementAt(int index);

Source Link

Document

Returns the value at the specified index.

Usage

From source file:Main.java

/**
 * Returns true if the given point is within the actual bounds of the
 * JList item at index (not just inside the cell).
 */// w w  w  .  j a  va  2  s . co m
private static boolean pointIsInActualBounds(JList list, int index, Point point) {
    ListCellRenderer renderer = list.getCellRenderer();
    ListModel dataModel = list.getModel();
    Object value = dataModel.getElementAt(index);
    Component item = renderer.getListCellRendererComponent(list, value, index, false, false);
    Dimension itemSize = item.getPreferredSize();
    Rectangle cellBounds = list.getCellBounds(index, index);
    if (!item.getComponentOrientation().isLeftToRight()) {
        cellBounds.x += (cellBounds.width - itemSize.width);
    }
    cellBounds.width = itemSize.width;

    return cellBounds.contains(point);
}

From source file:Main.java

public static java.util.List getListData(JList jlist) {
    java.util.List out = new ArrayList();
    ListModel model = jlist.getModel();
    for (int i = 0; i < model.getSize(); i++)
        out.add(model.getElementAt(i));
    return out;/* ww  w . j a v a 2  s.co m*/
}

From source file:Main.java

/**
 *
 * @param list/*from   www.ja  v a  2s .co  m*/
 * @param index
 * @param point
 * @return
 */
public static boolean pointIsInActualBounds(JList list, int index, Point point) {
    ListCellRenderer renderer = list.getCellRenderer();
    ListModel dataModel = list.getModel();
    Object value = dataModel.getElementAt(index);
    Component item = renderer.getListCellRendererComponent(list, value, index, false, false);
    Dimension itemSize = item.getPreferredSize();
    Rectangle cellBounds = list.getCellBounds(index, index);

    if (!item.getComponentOrientation().isLeftToRight()) {
        cellBounds.x += (cellBounds.width - itemSize.width);
    }

    cellBounds.width = itemSize.width;

    return cellBounds.contains(point);
}

From source file:Main.java

public static ArrayList listModelToList(ListModel listmodel) {
    ArrayList togo = new ArrayList();
    for (int i = 0; i < listmodel.getSize(); ++i) {
        togo.add(listmodel.getElementAt(i));
    }/*from   w  w  w  .  j  a va  2 s . co  m*/
    return togo;
}

From source file:Main.java

public static List<String> listModelToList(ListModel<String> modelo) {
    List<String> result = new ArrayList<>();

    for (int i = 0; i < modelo.getSize(); i++) {
        result.add(modelo.getElementAt(i));
    }//ww  w  .j av  a  2  s.c  o  m

    return result;
}

From source file:Main.java

public static <T extends Object> List<T> asList(ListModel listModel, Class<T> type) {

    List<T> res = new ArrayList<T>();
    for (int i = 0; i < listModel.getSize(); i++) {
        res.add(type.cast(listModel.getElementAt(i)));
    }//from ww w  .ja  va  2  s .  c  o m
    return res;
}

From source file:Main.java

public static int seachList(JList list, String match, int startIndex, boolean ingoreCase) {
    if (list == null || isStrEmpty(match) || list.getModel().getSize() <= 0 || startIndex < 0
            || startIndex >= list.getModel().getSize()) {
        return -1;
    }//from  ww  w . j a  v  a  2  s .  c om
    ListModel model = list.getModel();
    int searchCount = 0;
    for (; startIndex < model.getSize(); startIndex++) {
        if (ingoreCase) {
            if (model.getElementAt(startIndex).toString().toUpperCase().indexOf(match.toUpperCase()) >= 0) {
                return startIndex;
            }
        } else {
            if (model.getElementAt(startIndex).toString().indexOf(match) >= 0) {
                return startIndex;
            }
        }

        searchCount++;
        if (searchCount >= model.getSize()) {
            return -1;
        }
        if (startIndex >= model.getSize() - 1) {
            startIndex = -1;
        }
    }
    return -1;
}

From source file:com.palantir.ptoss.cinch.swing.JListWiringHarness.java

private static void updateListModel(JList list, List<?> newContents) {
    if (newContents == null) {
        newContents = ImmutableList.of();
    }/*from w  ww .  j  a v  a  2s .com*/
    ListModel oldModel = list.getModel();
    List<Object> old = Lists.newArrayListWithExpectedSize(oldModel.getSize());
    for (int i = 0; i < oldModel.getSize(); i++) {
        old.add(oldModel.getElementAt(i));
    }
    if (old.equals(newContents)) {
        return;
    }
    Object[] selected = list.getSelectedValues();
    DefaultListModel listModel = new DefaultListModel();
    for (Object obj : newContents) {
        listModel.addElement(obj);
    }
    list.setModel(listModel);
    List<Integer> newIndices = Lists.newArrayListWithCapacity(selected.length);
    Set<Object> selectedSet = Sets.newHashSet(selected);
    for (int i = 0; i < listModel.size(); i++) {
        if (selectedSet.contains(listModel.elementAt(i))) {
            newIndices.add(i);
        }
    }
    list.setSelectedIndices(ArrayUtils.toPrimitive(newIndices.toArray(new Integer[0])));
}

From source file:Main.java

public static <T extends Object> T[] extendModel(ListModel selected, T[] extras, Class<T> type) {

    int selectedSize = selected.getSize();
    int extraSize = extras.length;
    @SuppressWarnings("unchecked")
    T[] augmented = (T[]) Array.newInstance(type, selectedSize + extraSize);

    // copy current
    for (int i = 0; i < selectedSize; i++) {
        augmented[i] = type.cast(selected.getElementAt(i));
    }//  w w w .j  a  v  a  2s .com
    // augment
    for (int i = 0; i < extraSize; i++) {
        augmented[selectedSize + i] = extras[i];
    }
    return augmented;
}

From source file:SelectionHandler.java

private void removeFromSource(String item) {
    ListModel model = source.getModel();
    Vector listData = new Vector();
    for (int i = 0; i < model.getSize(); i++) {
        listData.addElement(model.getElementAt(i));
    }/*from ww  w  . java 2s .  com*/
    listData.removeElement(item);
    source.setListData(listData);
}