tags:

views:

64

answers:

2

I have a perfectly normal ArrayList<MyObject> that I need to edit and pick an object from.

In the application window, I have a JComboBox to select the appropriate choice from the list. I'm writing an editor dialog for these objects, which just includes a JList of these objects and editor fields. It's easy enough to do; I'll just have a ListModel implementation of some kind. Stick the ArrayList in, access it through the usual fields. The stuff in the GUI list is 1:1 representation of the stuff in the actual list. Easy.

But the combo box in the main application window is giving me a bit of a headache, because I need a special value. Ideally, the first item in the list should be "(None)", and return a null.

Do I just need to write some sort of weird ComboBoxModel implementation for this, or is there an easier, already implemented way to do this? I'd definitely imagine this sort of situation has cropped up before.

A: 

You might want to use a null-object. For example

public class MyObject {
   public static final MyObject NULL_OBJECT = new MyObject();

   ..
}

and then in your ArrayList just call:

arrayList.add(0, MyObject.NULL_OBJECT);

You null-object should have all of its properties set to null (or to some reasonable defaults), and your toString() method (if you are using it), should return "(none)" if all the fields are null.

Bozho
That's one way to do it, though it will cause some headaches when the ArrayList is saved. I'm using plain ordinary JAXB calls for this. Plus, if the arraylist has a null object as the first item, it will also probably show up in the list editor. This would probably involve a lot of manual additions and removals of the null object to work...
wwwwolf
ah, JAXB comes into the picture. Provide at least some parts of your code so that we can give adequate answers.
Bozho
+2  A: 

Implementing your own ComboBoxModel should be quite easy.

Since this solution creates a new Vector from your ArrayList, changes to yourArrayList after creating Vector won't be visible in your JComboBox. If you need this, then you'll have to implement your own ComboBoxModel (see DefaultComboBoxModel implementation).

You would have to do this anyway, since there is no DefaultComboBoxModel constructor that takes a List.

class SpecialComboBoxModel extends DefaultComboBoxModel {
    public final static String NULL_ELEMENT = "<None>";

    public SpecialComboBoxModel(Vector v) {
        super(v);
    }

    @Override
    public int getSize() {
        return super.getSize() + 1;
    }

    @Override
    public Object getElementAt(int index) {
        if( index == 0) {
            return NULL_ELEMENT;
        }
        return super.getElementAt(index - 1);
    }
}


    ArrayList<String> yourArrayList = new ArrayList<String>();
    yourArrayList.add("Value1");
    yourArrayList.add("Value2");
    Vector<String> v = new Vector<String>(yourArrayList);
    dropdown.setModel(new SpecialComboBoxModel(v));
Peter Lang
Sorry for a really slow reply, but thank you for this. I'll probably need more work on this code to bridge the ArrayList/Vector difference, but your solution was basically that I was looking for and seems to work right now.
wwwwolf