Java JComboBox Item replaceComboContents(JComboBox cb, String[] items)

Here you can find the source of replaceComboContents(JComboBox cb, String[] items)

Description

replace Combo Contents

License

BSD License

Declaration

public static void replaceComboContents(JComboBox cb, String[] items) 

Method Source Code


//package com.java2s;
// LICENSE:      This file is distributed under the BSD license.

import java.awt.event.ActionListener;
import javax.swing.JComboBox;

public class Main {
    public static void replaceComboContents(JComboBox cb, String[] items) {

        // remove listeners
        ActionListener[] listeners = cb.getActionListeners();
        for (int i = 0; i < listeners.length; i++)
            cb.removeActionListener(listeners[i]);

        if (cb.getItemCount() > 0)
            cb.removeAllItems();/*from   w w w .j av  a  2 s  .c  om*/

        // add contents
        for (int i = 0; i < items.length; i++) {
            cb.addItem(items[i]);
        }

        // restore listeners
        for (int i = 0; i < listeners.length; i++)
            cb.addActionListener(listeners[i]);
    }
}

Related

  1. items(JComboBox comboBox)
  2. loadCombo(JComboBox cmb, List data)
  3. loadInstances(JComboBox target, Class source, Class limit, Object defaultItem)
  4. loadStaticItems(JComboBox target, Class source, Class limit, String defaultItem)
  5. makeComboBox(Object[] items)
  6. replaceComboItems(JComboBox combo, Vector items)
  7. selJComboBoxItem(Properties properties, JComboBox combo, Vector namVec, String name)
  8. selJComboBoxItem(Properties properties, JComboBox combo, Vector namVec, String name)
  9. setListData(JComboBox box, Object[] items)