Package ucar.ui.prefs

Class ComboBox<E>

  • All Implemented Interfaces:
    ActionListener, ImageObserver, ItemSelectable, MenuContainer, Serializable, EventListener, Accessible, ListDataListener

    public class ComboBox<E>
    extends JComboBox<E>
    A simple extension to JComboBox, which persists the n latest values. The JComboBox is editable; user can add a new String, then if acceptable, the calling routine should transform it to correct object type and call addItem(). When item is added, it is placed on the top of the list, so it is more likely to be saved.

    The items in the list can be any Object type with these caveats:

    • item.toString() used as display name
    • item.equals() used for object equality
    • prefs.putBeanObject() used for storage, so XMLEncoder used, so object must have no-arg Constructor.
    When listening for change events, typically use addChangeListener(), which only throws an event on "comboBoxChanged". You must explicitly decide to save the selected Item in the list, eg on success. This moves it up the top of the saved list.
      cb.addChangeListener((e) -> {
          Object select = cb.getSelectedItem());
          if (isOK(select)) cb.addItem( select);
        });
     
    See Also:
    Serialized Form
    • Constructor Detail

      • ComboBox

        public ComboBox()
      • ComboBox

        public ComboBox​(PersistenceManager prefs)
        Constructor.
        Parameters:
        prefs - get/put list here; may be null.
      • ComboBox

        public ComboBox​(PersistenceManager prefs,
                        int nkeep)
        Constructor.
        Parameters:
        prefs - get/put list here; may be null.
        nkeep - keep this many when you save.
    • Method Detail

      • addChangeListener

        public void addChangeListener​(ActionListener successListener)
        Add a listener that gets called only when the selected item changes.
      • getDeepEditComponent

        public JComponent getDeepEditComponent()
      • addItem

        public void addItem​(E item)
        Add the item to the top of the list. If it already exists, move it to the top.
        Overrides:
        addItem in class JComboBox<E>
        Parameters:
        item - to be added.
      • save

        public void save()
        Save the last n items to PreferencesExt.
      • getItemList

        public List<E> getItemList()
        Use this to obtain the list of items.
        Returns:
        ArrayList of items, of type E.
      • setItemList

        public void setItemList​(Collection<E> list)
        Use this to set the list of items.
        Parameters:
        list - of items of type E.
      • setNkeep

        public void setNkeep​(int nkeep)
        Set the number of items to keep
      • getNkeep

        public int getNkeep()
        Get the number of items to keep
      • getStoreValue

        protected Object getStoreValue​(Object defValue)
        Get value from Store, will be an ArrayList or null
      • setStoreValue

        protected void setStoreValue​(List<E> newValue)
        Put new value into Store, must be a List of Strings