Search code examples
javaswingjcombobox

How to change that array of items in JComboBox at runtime


I add a JComboBox to a JPanel (in the JComboBox constructor I load array of strings). How can I change that array at runtime, to load new items?


Solution

  • Rather than pass your values in the constructor, you should create a ComboBoxModel derived class and pass that to the constructor. A simple example would be DefaultComboBoxModel, but you could create your own if that didn't satisfy your needs. Then you can modify the model and the changes are applied to the ComboBox.