Java JComboBox setPopupComboChoices(JComboBox aComboBox, String aS)

Here you can find the source of setPopupComboChoices(JComboBox aComboBox, String aS)

Description

Routine to strip all possible choices from a "popup" string

Example of a popup string: "file|socket|ethernet;socket"
The | splitted values are the possible combochoices
The ; splitted value is the default choice.

License

Open Source License

Parameter

Parameter Description
aComboBox the JComboBox that need to be set
aS the string that contains the possible choices

Declaration

static public void setPopupComboChoices(JComboBox aComboBox, String aS) 

Method Source Code

//package com.java2s;
/*// w w  w.  ja  v a2s  .c  om
 * 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.JComboBox;

public class Main {
    /** Routine to strip all possible choices from a "popup" string <br><br>
     * Example of a popup string: "file|socket|ethernet;socket"<br> 
     * The | splitted values are the possible combochoices<br>
     * The ; splitted value is the default choice.
     * 
     * @param aComboBox the JComboBox that need to be set
     * @param aS the string that contains the possible choices
     */
    static public void setPopupComboChoices(JComboBox aComboBox, String aS) {
        aComboBox.removeAllItems();
        if (aS.length() < 1) {
            return;
        }
        // first strip input on ; because after the ; a default choice has been given.
        String[] stripped = aS.split("\\;");
        String defaultChoice = "";
        if (stripped.length > 1) {
            defaultChoice = stripped[1];
        }

        // now determine all possible choices and fill the combobox
        String[] choices = stripped[0].split("\\|");
        for (int i = 0; i < choices.length; i++) {
            if (!choices[i].equals("")) {
                aComboBox.addItem(choices[i]);
            }
        }

        // set the default choice (if any)
        if (!defaultChoice.equals("")) {
            aComboBox.setSelectedItem(defaultChoice);
        } else {
            aComboBox.setSelectedIndex(0);
        }
        aComboBox.validate();
    }
}

Related

  1. resetPhaseBox(JComboBox phaseBox)
  2. setColorMenu(JComboBox choice)
  3. setComboBoxTheme(JComboBox comboBox)
  4. setErrorBackground(JComboBox comp)
  5. setJComboBoxAsHeavyweight(JComboBox combo)
  6. setShouldUseAlternSdk(boolean shouldUse, final JCheckBox useAlternativeSdkCB, final JComboBox alternativeSdksComboBox, final JComboBox myModulesComboBox)
  7. setupComboBoxMaxRows( T inComboBox)
  8. updateContents( @SuppressWarnings("rawtypes") JComboBox box, Object[] options)
  9. updateGooseChooser(JComboBox gooseChooser, String callingGoose, String[] allGeese)