Skip to main content

cant clear ComboBox model..

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
3 replies [Last post]
68060
Offline
Joined: 2011-08-29

this code:

for (int i=0; i<cb.getModel().getSize(); i++)
{
cb.getModel().removeItem(i);
}

leave sone entry in my combobox somehow!?

only if theres like 3+ entries, anyway i think its a bug, where do i submit :)

same for anyone else?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Chen Fishbein Guest
Offline
Joined: 2010-12-07

you are removing items and the getSize is being modified while you loop

try this instead:

while(cb.getModel().getSize() > 0) {
removeItem(0);
}

On 07/09/2011 16:44, forums@java.net wrote:
> this code:
>
>
>
> for (int i=0; i {
> cb.getModel().removeItem(i);
> }
>
>
>
> leave sone entry in my combobox somehow!?
>
>
>
> same for anyone else?
>
>

vprise
Offline
Joined: 2003-11-07

3 items i = 0. You remove item 0 and now have items 0, 1

2 items i = 1 and size = 2. You remove item 1.

1 item i = 2 and size = 1. Loop exits.

I would just do: cb.setModel(new DefaultListModel());

68060
Offline
Joined: 2011-08-29

haha! thanks guys, or i could do the getsize before my loop i presume, thanks i really didnt notice this problem :)