tags:

views:

41

answers:

1

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?

+2  A: 

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.

camickr
Yes it was. I guess I was thrown by the `Note: Be careful when implementing a custom model for a combo box. The JComboBox methods that change the items in the combo box's menu, such as insertItemAt, work only if the data model implements the MutableComboBoxModel interface (a subinterface of ComboBoxModel). Refer to the API tables to see which methods are affected. in `http://download.oracle.com/javase/tutorial/uiswing/components/combobox.html All that is really needed to add String string is `jComboBoxImei.addItem(string)`
JackN