Here you can find the source of fillSelectionListFromString(JList aListComponent, String theList, boolean removeQuotes)
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 |
static public void fillSelectionListFromString(JList aListComponent, String theList, boolean removeQuotes)
//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(); } } } }