Java JList Select fillSelectionListFromString(JList aListComponent, String theList, boolean removeQuotes)

Here you can find the source of fillSelectionListFromString(JList aListComponent, String theList, boolean removeQuotes)

Description

Selects items in a JList component using a String representation of the to-be selected contents

Example of input argument theList with !removeQuotes : ["123","456","789"]
Example of input argument theList with removeQuotes: [123,456,789]

License

Open Source License

Parameter

Parameter Description
aListComponent the JList component where the data has to be inserted in.
theList the String representation of the selected items in a JList component
removeQuotes removes/does not remove quotes from each individual value in the theList input

Declaration

static public void fillSelectionListFromString(JList aListComponent,
        String theList, boolean removeQuotes) 

Method Source Code

//package com.java2s;
/*//  w w w .j  a v  a 2  s .  c o m
 * LofarUtils.java
 *
 *  Copyright (C) 2002-2007
 *  ASTRON (Netherlands Foundation for Research in Astronomy)
 *  P.O.Box 2, 7990 AA Dwingeloo, The Netherlands, seg@astron.nl
 *
 *  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
 *
 */

import javax.swing.JList;

public class Main {
    /**
     * Selects items in a JList component using a String representation of the to-be selected contents<br><br>
     * Example of input argument theList with !removeQuotes : ["123","456","789"]<br>
     * Example of input argument theList with removeQuotes: [123,456,789]
     *
     * @param aListComponent the JList component where the data has to be inserted in.
     * @param theList the String representation of the selected items in a JList component
     * @param removeQuotes removes/does not remove quotes from each individual value in the theList input
     */
    static public void fillSelectionListFromString(JList aListComponent,
            String theList, boolean removeQuotes) {
        String aList = theList;
        if (aList.startsWith("[")) {
            aList = aList.substring(1, aList.length());
        }
        if (aList.endsWith("]")) {
            aList = aList.substring(0, aList.length() - 1);
        }
        if (!aList.equals("")) {
            String[] aS = aList.split("\\,");
            String[] toBeSelectedValues = new String[aS.length];
            for (int i = 0; i < aS.length; i++) {
                if (removeQuotes) {
                    toBeSelectedValues[i] = aS[i].substring(1,
                            aS[i].length() - 1);
                } else {
                    toBeSelectedValues[i] = aS[i];
                }
            }
            int[] toBeSelectedIndices = new int[toBeSelectedValues.length];
            int aValueIndex = 0;
            if (toBeSelectedValues.length > 0) {
                for (String aValue : toBeSelectedValues) {

                    for (int in = 0; in < aListComponent.getModel()
                            .getSize(); in++) {
                        String aCorrType = (String) aListComponent
                                .getModel().getElementAt(in);
                        if (aValue.equals(aCorrType)) {
                            toBeSelectedIndices[aValueIndex] = in;
                        }
                    }
                    aValueIndex++;
                }
                aListComponent.setSelectedIndices(toBeSelectedIndices);

            } else {
                aListComponent.clearSelection();
            }
        }
    }
}

Related

  1. applyDefaultDisplaySettings(JList list, int index, boolean selected, boolean cellHasFocus, JComponent renderer)
  2. canMoveSelectedItemsUp(JList list)
  3. createStringFromSelectionList( JList aListComponent, boolean createQuotes)
  4. downListSelectedIndex(JList sourceList)
  5. ensureSelectionIsVisible(final JList list)
  6. fireSelectRow(final JList list, final Object value)
  7. fireSelectRows(final JList list, final int[] rows)
  8. getSelectedIndecies(final ListSelectionModel lsm)
  9. getSelectedItems(DefaultListModel listModel, ListSelectionModel selectionModel)