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