JComboBox: want first entry to be blank entry

daverocks picture daverocks · Jun 8, 2011 · Viewed 30.7k times · Source

I have a jcombobox which values come from a list. I want to first value to be blank from dropdown list. The way I approached it was by putting new object of the list type in first as example shows :

 final List<Object> list = getObjectsList();
  list.add(new Object()); 

But this results in null pointer and if do

list.add(null);

this solves the issue but then gives me another prob elsewhere with a Comparator method. So any work a rounds would be great thanks.

Answer

user1494530 picture user1494530 · Aug 19, 2013

You can also set the selected index to -1 after the items have been added, but before the event listener.

    JcomboBox.setSelectedIndex(-1);

This solution will work better if your JcomboBox uses generics.