Example usage for javax.swing JList getModel

List of usage examples for javax.swing JList getModel

Introduction

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

Prototype

public ListModel<E> getModel() 

Source Link

Document

Returns the data model that holds the list of items displayed by the JList component.

Usage

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JList list = new JList(new CheckListItem[] { new CheckListItem("apple"), new CheckListItem("orange"),
            new CheckListItem("mango"), new CheckListItem("mango"), new CheckListItem("mango"),
            new CheckListItem("mango"), new CheckListItem("mango"), new CheckListItem("mango"),
            new CheckListItem("mango"), new CheckListItem("mango"), new CheckListItem("mango"),

            new CheckListItem("paw paw"), new CheckListItem("banana") });
    list.setCellRenderer(new CheckListRenderer());
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    list.addMouseListener(new MouseAdapter() {
        @Override//from w w w. j a v  a 2s  . com
        public void mouseClicked(MouseEvent event) {
            JList list = (JList) event.getSource();
            int index = list.locationToIndex(event.getPoint());// Get index of item
                                                               // clicked
            CheckListItem item = (CheckListItem) list.getModel().getElementAt(index);
            item.setSelected(!item.isSelected()); // Toggle selected state
            list.repaint(list.getCellBounds(index, index));// Repaint cell
        }
    });
    frame.getContentPane().add(new JScrollPane(list));
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

/**
 * Get the DefaultListModel from JList.//from   w  w  w .  ja v a  2 s. c  o m
 *
 * @param list
 *
 * @return Forcely Type casted to DefaultListModel
 */
public static <T> DefaultListModel<T> getDefaultListModel(JList<T> list) {
    return (DefaultListModel<T>) list.getModel();
}

From source file:Main.java

public static void setSelection(JList list, Object element) {
    for (int i = 0; i < list.getModel().getSize(); i++) {
        Object value = (Object) list.getModel().getElementAt(i);
        if (value == element) {
            list.setSelectedIndex(i);//from   w w  w  . j ava 2 s  . c o  m
            list.scrollRectToVisible(list.getCellBounds(i, i));
            return;
        }
    }
}

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));// w  w w  .  j a v a  2s . c om
    return out;
}

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   w  w  w .  j av  a2 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:Main.java

/**
 * Returns true if the given point is within the actual bounds of the
 * JList item at index (not just inside the cell).
 *///from   w  w w  .  ja  v a2 s.  c o 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

/**
 *
 * @param list//www. j  av  a2 s . c o 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

/**
 * checks whether the selected items can be moved down
 *
 * @param list        the JList to work on
 */// ww  w.  j  a v a2  s.co  m
public static boolean canMoveDown(JList list) {
    boolean result;
    int[] indices;

    result = false;

    indices = list.getSelectedIndices();
    if (indices.length > 0) {
        if (indices[indices.length - 1] < list.getModel().getSize() - 1)
            result = true;
    }

    return result;
}

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

private static void updateListModel(JList list, List<?> newContents) {
    if (newContents == null) {
        newContents = ImmutableList.of();
    }//  ww w.  j  a  v  a  2  s . c o  m
    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:tourma.utils.web.WebStatistics.java

/**
 * //w  w w .j  a v a2s  .  co m
 */
public static String getHTML() {
    StringBuffer stats = new StringBuffer("");

    JPNStatistics jpn = new JPNStatistics();
    jpn.setSize(640, 480);
    JTabbedPane jtp = jpn.getTabbedPane();
    for (int i = 0; i < jtp.getTabCount(); i++) {
        Component comp = jtp.getComponent(i);

        if (comp instanceof ChartPanel) {
            ChartPanel panel = (ChartPanel) comp;
            panel.setSize(640, 480);
            BufferedImage buf = new BufferedImage(640, 480, BufferedImage.TYPE_INT_ARGB);
            Graphics g = buf.createGraphics();
            panel.print(g);
            g.dispose();

            //BufferedImage buf = toBufferedImage(img, 640, 480);
            String img_str = WebPicture.getPictureAsHTML(buf, 640, 480, true);
            stats.append(img_str);
        }

        if (comp instanceof JPanel) {
            // Find JList, Select All then Find ChartPanel
            JPanel pane = (JPanel) comp;
            ChartPanel panel = null;
            JList list = null;
            for (int j = 0; j < pane.getComponentCount(); j++) {
                Component c = pane.getComponent(j);
                if (c instanceof JScrollPane) {
                    for (int k = 0; k < ((JScrollPane) c).getViewport().getComponentCount(); k++) {
                        Component c2 = ((JScrollPane) c).getViewport().getComponent(k);
                        if (c2 instanceof JList) {
                            list = (JList) c2;
                        }

                    }
                }
            }

            if (list != null) {

                int start = 0;
                int end = list.getModel().getSize() - 1;
                if (end >= 0) {
                    list.setSelectionInterval(start, end);
                }

                jpn.updatePositions();
            }
            for (int j = 0; j < pane.getComponentCount(); j++) {
                Component c = pane.getComponent(j);
                if (c instanceof ChartPanel) {
                    panel = (ChartPanel) c;
                }
            }

            if (panel != null) {
                panel.setSize(640, 480);
                BufferedImage buf = new BufferedImage(640, 480, BufferedImage.TYPE_INT_ARGB);
                Graphics g = buf.createGraphics();
                panel.print(g);
                g.dispose();

                //BufferedImage buf = toBufferedImage(img, 640, 480);
                String img_str = WebPicture.getPictureAsHTML(buf, 640, 480, true);
                stats.append(img_str);
            }
        }
    }

    return stats.toString();
}