Java JList Create createList(JList aListComponent, boolean createQuotes)

Here you can find the source of createList(JList aListComponent, boolean createQuotes)

Description

Creates a String representation of the contents of a JList component

Example of output with createQuotes : ["123","456","789"]
Example of output with !createQuotes: [123,456,789]
Returns '[]' when no items were detected in the JList.

License

Open Source License

Parameter

Parameter Description
aListComponent the JList component of which the data has to be converted
createQuotes add/do not add quotes to each individual value in the string output

Return

String representation of aListComponent

Declaration

static public String createList(JList aListComponent,
        boolean createQuotes) 

Method Source Code

//package com.java2s;
/*/*from  w  w  w.j  a v a2 s.  co 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 {
    /**
     * Creates a String representation of the contents of a JList component<br><br>
     * Example of output with createQuotes : ["123","456","789"]<br>
     * Example of output with !createQuotes: [123,456,789]<br>
     * Returns '[]' when no items were detected in the JList.
     *
     * @param aListComponent the JList component of which the data has to be converted
     * @param createQuotes add/do not add quotes to each individual value in the string output
     * @return String representation of aListComponent
     */
    static public String createList(JList aListComponent,
            boolean createQuotes) {
        String aList = "[";
        if (aListComponent.getModel().getSize() > 0) {
            if (createQuotes) {
                aList += "\"";
            }
            aList += (String) aListComponent.getModel().getElementAt(0);
            if (createQuotes) {
                aList += "\"";
            }
            for (int i = 1; i < aListComponent.getModel().getSize(); i++) {
                aList += ",";
                if (createQuotes) {
                    aList += "\"";
                }
                aList += aListComponent.getModel().getElementAt(i);
                if (createQuotes) {
                    aList += "\"";
                }
            }

        }
        aList += "]";
        return aList;
    }
}

Related

  1. createJScrollPane(JList list)
  2. createListPane(JList list, String text)