Here you can find the source of updateGooseChooser(JComboBox gooseChooser, String callingGoose, String[] allGeese)
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 |
public static void updateGooseChooser(JComboBox gooseChooser, String callingGoose, String[] allGeese)
//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); } }