Java JComboBox updateGooseChooser(JComboBox gooseChooser, String callingGoose, String[] allGeese)

Here you can find the source of updateGooseChooser(JComboBox gooseChooser, String callingGoose, String[] allGeese)

Description

Updates the UI to show the current list of geese.

License

Open Source License

Parameter

Parameter Description
gooseChooser The UI element containing the list of geese
callingGoose The name of the calling goose
allGeese The list of all currently active geese

Declaration

public static void updateGooseChooser(JComboBox gooseChooser, String callingGoose, String[] allGeese) 

Method Source Code

//package com.java2s;
/*//from www  .  j  a  v  a 2  s . c  om
 * MiscUtil.java
 * miscellaneous utilities
 * Copyright (C) 2006 by Institute for Systems Biology,
 * Seattle, Washington, USA.  All rights reserved.
 *
 * This source code is distributed under the GNU Lesser
 * General Public License, the text of which is available at:
 *   http://www.gnu.org/copyleft/lesser.html
 */

import java.util.Arrays;

import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

public class Main {
    /**
     * Updates the UI to show the current list of geese. Removes the name of the
     * calling goose from the list, and sorts the remainder. Tries to preserve the
     * original selection in the list, if it still exists in the newly received
     * current goose list.
     * @param gooseChooser The UI element containing the list of geese
     * @param callingGoose The name of the calling goose
     * @param allGeese The list of all currently active geese
     */
    public static void updateGooseChooser(JComboBox gooseChooser, String callingGoose, String[] allGeese) {
        if (gooseChooser == null || allGeese == null)
            return;
        String savedItem = (String) gooseChooser.getSelectedItem();

        Arrays.sort(allGeese);
        DefaultComboBoxModel model = (DefaultComboBoxModel) gooseChooser.getModel();
        model.removeAllElements();
        model.addElement("Boss");

        for (String gooseName : allGeese) {
            if (!gooseName.equals(callingGoose)) {
                model.addElement(gooseName);
            }
        }
        // this will attempt to set selected item to what it was before.
        // if that item is no longer in the list, it will silently fail
        // and therefore Boss, as the first item, will be selected by default.
        if (savedItem != null)
            gooseChooser.setSelectedItem(savedItem);
    }
}

Related

  1. setJComboBoxAsHeavyweight(JComboBox combo)
  2. setPopupComboChoices(JComboBox aComboBox, String aS)
  3. setShouldUseAlternSdk(boolean shouldUse, final JCheckBox useAlternativeSdkCB, final JComboBox alternativeSdksComboBox, final JComboBox myModulesComboBox)
  4. setupComboBoxMaxRows( T inComboBox)
  5. updateContents( @SuppressWarnings("rawtypes") JComboBox box, Object[] options)