Java JList Select createStringFromSelectionList( JList aListComponent, boolean createQuotes)

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

Description

Creates a String representation of the selected items in a JList component

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

License

Open Source License

Parameter

Parameter Description
aListComponent the JList component of which the selected items have to be in a String
createQuotes add/do not add quotes to each individual value in the string output

Return

String representation of the selected items in a aListComponent

Declaration

static public String createStringFromSelectionList(
        JList aListComponent, boolean createQuotes) 

Method Source Code

//package com.java2s;
/*//  w w  w. j  a  va2s  . com
 * 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 <b><i>selected</i></b> items in 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 selected items were detected in the JList.
     * 
     * @param aListComponent the JList component of which the selected items have to be in a String
     * @param createQuotes add/do not add quotes to each individual value in the string output
     * @return String representation of the selected items in a aListComponent
     */
    static public String createStringFromSelectionList(
            JList aListComponent, boolean createQuotes) {
        String aList = "[";
        if (aListComponent.getModel().getSize() > 0) {
            int[] selectedIndices = aListComponent.getSelectedIndices();
            for (int i = 0; i < selectedIndices.length; i++) {
                if (i > 0) {
                    aList += ",";
                }
                if (createQuotes) {
                    aList += "\"";
                }
                aList += aListComponent.getModel().getElementAt(
                        selectedIndices[i]);
                if (createQuotes) {
                    aList += "\"";
                }
            }
        }
        aList += "]";
        return aList;
    }
}

Related

  1. adaptToList(final JComponent renderer, JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
  2. applyDefaultDisplaySettings(JList list, int index, boolean selected, boolean cellHasFocus, JComponent renderer)
  3. canMoveSelectedItemsUp(JList list)
  4. downListSelectedIndex(JList sourceList)
  5. ensureSelectionIsVisible(final JList list)
  6. fillSelectionListFromString(JList aListComponent, String theList, boolean removeQuotes)
  7. fireSelectRow(final JList list, final Object value)