Example usage for javax.swing JList getSelectedIndices

List of usage examples for javax.swing JList getSelectedIndices

Introduction

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

Prototype

@Transient
public int[] getSelectedIndices() 

Source Link

Document

Returns an array of all of the selected indices, in increasing order.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String[] items = { "A", "B", "C", "D" };
    JList list = new JList(items);
    // Get the index of all the selected items
    int[] selectedIx = list.getSelectedIndices();

    // Get all the selected items using the indices
    for (int i = 0; i < selectedIx.length; i++) {
        Object sel = list.getModel().getElementAt(selectedIx[i]);
    }/*from   ww  w.  ja v a2s .  c o m*/

    // Get the index of the first selected item
    int firstSelIx = list.getSelectedIndex();

}

From source file:Main.java

public static void main(String args[]) {
    String labels[] = { "A", "B", "C", "D", "E", "F", "G", "H" };
    JFrame frame = new JFrame("Selecting JList");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JList jlist = new JList(labels);
    JScrollPane scrollPane1 = new JScrollPane(jlist);
    frame.add(scrollPane1, BorderLayout.CENTER);

    ListSelectionListener listSelectionListener = new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent listSelectionEvent) {
            System.out.println("First index: " + listSelectionEvent.getFirstIndex());
            System.out.println(", Last index: " + listSelectionEvent.getLastIndex());
            boolean adjust = listSelectionEvent.getValueIsAdjusting();
            System.out.println(", Adjusting? " + adjust);
            if (!adjust) {
                JList list = (JList) listSelectionEvent.getSource();
                int selections[] = list.getSelectedIndices();
                Object selectionValues[] = list.getSelectedValues();
                for (int i = 0, n = selections.length; i < n; i++) {
                    if (i == 0) {
                        System.out.println(" Selections: ");
                    }//w  w w  . j a va2  s.c o m
                    System.out.println(selections[i] + "/" + selectionValues[i] + " ");
                }
            }
        }
    };
    jlist.addListSelectionListener(listSelectionListener);

    frame.setSize(350, 200);
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(final String args[]) {
    final String labels[] = { "A", "B", "C", "D", "E" };
    JFrame frame = new JFrame("Selecting JList");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JList jlist = new JList(labels);
    JScrollPane scrollPane1 = new JScrollPane(jlist);
    frame.add(scrollPane1);//from   ww  w .j ava  2 s. c  om

    ListSelectionListener listSelectionListener = new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent listSelectionEvent) {
            System.out.println("First index: " + listSelectionEvent.getFirstIndex());
            System.out.println(", Last index: " + listSelectionEvent.getLastIndex());
            boolean adjust = listSelectionEvent.getValueIsAdjusting();
            System.out.println(", Adjusting? " + adjust);
            if (!adjust) {
                JList list = (JList) listSelectionEvent.getSource();
                int selections[] = list.getSelectedIndices();
                Object selectedValues[] = list.getSelectedValues();
                for (int i = 0, n = selections.length; i < n; i++) {
                    if (i == 0) {
                        System.out.println("  Selections: ");
                    }
                    System.out.println(selections[i] + "/" + selectedValues[i] + " ");
                }
            }
        }
    };
    jlist.addListSelectionListener(listSelectionListener);

    frame.setSize(350, 200);
    frame.setVisible(true);
}

From source file:SelectingJListSample.java

public static void main(String args[]) {
    String labels[] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" };
    JFrame frame = new JFrame("Selecting JList");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    JList jlist = new JList(labels);
    JScrollPane scrollPane1 = new JScrollPane(jlist);
    contentPane.add(scrollPane1, BorderLayout.WEST);

    ListSelectionListener listSelectionListener = new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent listSelectionEvent) {
            System.out.print("First index: " + listSelectionEvent.getFirstIndex());
            System.out.print(", Last index: " + listSelectionEvent.getLastIndex());
            boolean adjust = listSelectionEvent.getValueIsAdjusting();
            System.out.println(", Adjusting? " + adjust);
            if (!adjust) {
                JList list = (JList) listSelectionEvent.getSource();
                int selections[] = list.getSelectedIndices();
                Object selectionValues[] = list.getSelectedValues();
                for (int i = 0, n = selections.length; i < n; i++) {
                    if (i == 0) {
                        System.out.print("  Selections: ");
                    }//from   ww  w .  j  a  va2s.c o m
                    System.out.print(selections[i] + "/" + selectionValues[i] + " ");
                }
                System.out.println();
            }
        }
    };
    jlist.addListSelectionListener(listSelectionListener);

    MouseListener mouseListener = new MouseAdapter() {
        public void mouseClicked(MouseEvent mouseEvent) {
            JList theList = (JList) mouseEvent.getSource();
            if (mouseEvent.getClickCount() == 2) {
                int index = theList.locationToIndex(mouseEvent.getPoint());
                if (index >= 0) {
                    Object o = theList.getModel().getElementAt(index);
                    System.out.println("Double-clicked on: " + o.toString());
                }
            }
        }
    };
    jlist.addMouseListener(mouseListener);

    frame.setSize(350, 200);
    frame.setVisible(true);
}

From source file:Main.java

/**
 * checks whether the selected items can be moved up
 *
 * @param list        the JList to work on
 *///from   ww  w.j a  v a 2s.com
public static boolean canMoveUp(JList list) {
    boolean result;
    int[] indices;

    result = false;

    indices = list.getSelectedIndices();
    if (indices.length > 0) {
        if (indices[0] > 0)
            result = true;
    }

    return result;
}

From source file:Main.java

/**
 * checks whether the selected items can be moved down
 *
 * @param list        the JList to work on
 *///from   w ww .jav a  2 s . c  o  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:io.neocdtv.simpleplayer.ui.PlaylistTransferHandler.java

protected String exportString(JComponent c) {
    JList list = (JList) c;
    indices = list.getSelectedIndices();
    List values = list.getSelectedValuesList();

    StringBuilder buff = new StringBuilder();

    for (int i = 0; i < values.size(); i++) {
        Object val = values.get(i);
        buff.append(val == null ? "" : val.toString());
        if (i != values.size() - 1) {
            buff.append("\n");
        }//from   w ww.  j a  v  a 2 s .c  o  m
    }

    return buff.toString();
}

From source file:ExtendedDnDDemo.java

protected String exportString(JComponent c) {
    JList list = (JList) c;
    indices = list.getSelectedIndices();
    Object[] values = list.getSelectedValues();

    StringBuffer buff = new StringBuffer();

    for (int i = 0; i < values.length; i++) {
        Object val = values[i];
        buff.append(val == null ? "" : val.toString());
        if (i != values.length - 1) {
            buff.append("\n");
        }//from  w w  w .j av  a 2  s.c om
    }

    return buff.toString();
}

From source file:edu.ku.brc.specify.tasks.subpane.VisualQueryPanel.java

/**
 * Moves selected items from one list to the other.
 * @param srcList//  w w w  .ja  va 2s . c  o m
 * @param srcHash
 * @param dstList
 * @param dstHash
 */
private void moveItems(final JList srcList, final HashSet<Integer> srcHash, final JList dstList,
        final HashSet<Integer> dstHash) {
    int inx = srcList.getSelectedIndex();
    if (inx > -1) {
        DefaultListModel srcModel = (DefaultListModel) srcList.getModel();
        DefaultListModel dstModel = (DefaultListModel) dstList.getModel();

        int[] indexes = srcList.getSelectedIndices();
        ArrayList<LatLonPoint> llpList = new ArrayList<LatLonPoint>(indexes.length);
        for (int selInx : indexes) {
            LatLonPoint llp = (LatLonPoint) srcModel.get(selInx);
            llpList.add(llp);

            if (!dstHash.contains(llp.getLocId())) {
                dstModel.addElement(llp);
                dstHash.add(llp.getLocId());
            }
        }

        for (LatLonPoint llp : llpList) {
            srcModel.removeElement(llp);
            srcHash.remove(llp.getLocId());
        }
    }
}

From source file:edu.ku.brc.specify.config.init.secwiz.UserPanel.java

/**
 * @param doGainAccess//from www  .  ja v  a 2 s  .  c  o  m
 */
private void changeMasterAccess(final boolean doGainAccess) {
    String msg = UIRegistry.getResourceString(doGainAccess ? "DO_GAIN_ACCESS" : "DO_LOOSE_ACCESS");
    if (UIRegistry.askYesNoLocalized("YES", "NO", msg, "WARNING") == JOptionPane.NO_OPTION) {
        return;
    }

    DBMSUserMgr mgr = DBMSUserMgr.getInstance();

    String dbUserName = properties.getProperty("dbUserName");
    String dbPassword = properties.getProperty("dbPassword");
    String saUserName = properties.getProperty("saUserName");
    String hostName = properties.getProperty("hostName");

    String dbName = doGainAccess ? otherDBName : databaseName;

    if (mgr.getConnection() == null) {
        if (!mgr.connectToDBMS(dbUserName, dbPassword, hostName)) {
            UIRegistry.showError("Unable to login.");
            return;
        }
    }

    ArrayList<String> changedNames = new ArrayList<String>();
    ArrayList<String> noChangeNames = new ArrayList<String>();

    JList list = doGainAccess ? otherDBList : dbList;
    int[] inxs = list.getSelectedIndices();
    for (int inx : inxs) {
        String dbNm = (String) list.getModel().getElementAt(inx);
        if (mgr.setPermissions(saUserName, dbNm,
                doGainAccess ? DBMSUserMgr.PERM_ALL_BASIC : DBMSUserMgr.PERM_NONE)) {
            changedNames.add(dbNm);
        } else {
            noChangeNames.add(dbNm);
        }
    }

    for (String nm : changedNames) {
        if (doGainAccess) {
            ((DefaultListModel) otherDBList.getModel()).removeElement(nm);
            ((DefaultListModel) dbList.getModel()).addElement(nm);
        } else {
            ((DefaultListModel) otherDBList.getModel()).addElement(nm);
            ((DefaultListModel) dbList.getModel()).removeElement(nm);
        }
    }

    if (inxs.length == 1) {
        UIRegistry.showLocalizedMsg(JOptionPane.INFORMATION_MESSAGE, "MSTR_PERM_CHGED_TITLE",
                doGainAccess ? "MSTR_PERM_ADDED" : "MSTR_PERM_DEL", saUserName, dbName);
        final int selInx = inxs[0];
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                dbList.setSelectedIndex(selInx);
            }
        });
    } else {
        UIRegistry.showLocalizedMsg(JOptionPane.INFORMATION_MESSAGE, "MSTR_PERM_CHGED_TITLE",
                doGainAccess ? "MSTR_NUM_PERM_ADDED" : "MSTR_NUM_PERM_DEL", saUserName, changedNames.size());
    }

    mgr.close();
}