Java JList Add Remove fillList(JList aListComponent, String theList, boolean removeQuotes)

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

Description

Fills a JList component with a String representation of the to-be 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 a JList component
removeQuotes removes/does not remove quotes from each individual value in the theList input

Declaration

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

Method Source Code

//package com.java2s;
/*/*from  ww  w .j ava  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.DefaultListModel;

import javax.swing.JList;

public class Main {
    /**
     * Fills a JList component with a String representation of the to-be 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 a JList component
     * @param removeQuotes removes/does not remove quotes from each individual value in the theList input
     */
    static public void fillList(JList aListComponent, String theList,
            boolean removeQuotes) {
        DefaultListModel itsModel = new DefaultListModel();
        aListComponent.setModel(itsModel);
        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("\\,");
            for (int i = 0; i < aS.length; i++) {
                if (removeQuotes) {
                    itsModel.addElement(aS[i].substring(1,
                            aS[i].length() - 1));
                } else {
                    itsModel.addElement(aS[i]);
                }
            }
            aListComponent.setModel(itsModel);
        }
    }
}

Related

  1. addDoubleClickEvent(JList list)
  2. addItemJList(javax.swing.JList jlist, String item)
  3. addToList(JList list, Object object)
  4. fillList(Collection anList, JList anJList)
  5. fillList(Object[] elements, JList list)
  6. JListAddObject(javax.swing.JList list, Object item)
  7. removeAllListItems(JList sourceList)
  8. removeFromList(JList list, Object object)