开发者

JComboBox: want first entry to be blank entry

开发者 https://www.devze.com 2023-03-11 00:17 出处:网络
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 :

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.


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.


Doesn't JComboBox.insertItemAt("", 0); work for you? You need to add validation for the blank entry too


IMHO it depends from what will you do with this "empty entry". Few ideas:

Try add empty string.

Add object/null AND override getSelectedIndex() method.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号