I need to create a combo box that can be modified by the user on the fly. I was able to do this in the android environment (Swing ComboBoxes seem to be the same as Android spinners) like this:
final Spinner spinner = (Spinner) findViewById(R.id.spinnerI);
String[] strings = configuration.getNames();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, strings);
adapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
How can I do something similar using Java Swing? Should even use the ComboBox in the Swing Palette? When I do a
jComboBoxImei.setModel(new javax.swing.MutableComboBoxModel()
after initComponents(), JavaBeans wants me to implement all abstract methods (addElement(), removeElement(), …). What is the simplest way to implement a dynamic ComboBox using Java and/or Swing?
There is no need to implement a custom model. You can use the DefaultComboBoxModel which supports add/remove methods.
JComboBox also has add/remove methods that allows you to dynamically add/remove items when you use a mutable model.