wesley.hall@gmail.com schrieb:
> > You could remove the listeners before updating the ComboBox, then restore them
> > after the update is complete:
> In any case, I dont think it will solve the OPs problem. Calling
> 'removeAllItems' will result in the selected item being lost.
I know this issue, I restore the selected item after updating the
contents.
setup_cbport.setSelectedItem(port); // previously selected item
After all, the modification of the combobox is not a bad idea.
Vector<String> set=new Vector<String>();
while (...) {
// collect items from the OS
set.add(pId.getName());
}
// delete items from the box
Vector<String> set2=new Vector<String>();
int i=0; while (i<setup_cbport.getItemCount()) {
String itm=(String)setup_cbport.getItemAt(i);
if (!set.contains(itm)) setup_cbport.removeItemAt(i);
else {set2.add(itm); i++;}
}
// add new items
Iterator it = set.iterator();
while (it.hasNext()) {
Object itm=it.next();
if (!set2.contains(itm)) setup_cbport.addItem(itm);
}
wesley.hall@gmail.com - 15 Dec 2006 14:03 GMT
J?rgen Gerstacker wrote:
> wesley.hall@gmail.com schrieb:
>
[quoted text clipped - 6 lines]
> I know this issue, I restore the selected item after updating the
> contents.
There is more to it than this, removing the selected item sets into
motion a whole series of events that is what ultimately ends up in your
actionPerformed event being fired. This happens before your remove
method invocation even returns. Restoring the selected item later will
not prevent it.
The slightly lazy approach, is what Nigel suggested. Remove the
listeners, then add them back after you are done with your
manipulation. I stand by my point that this is a bad bad habbit, but I
would be lying if I said I haven't done it myself and it (now that I
understand a little more) should solve your problem for now.
My point, is that the better way is to take control over which events
are fired between your model and your view. This can be accomplished by
creating your own model rather than using the default one. As you get
into more complex swing code, you will eventually have no choice but to
create your own models. In this example, you do have a choice, but it
is a choice between lots of ugly code that is doing far more work than
required, or the elegant solution where you use the libraries how Sun
originally intended.
The more elegant solution may require for you to learn a little more
about MVC and how it works within Swing, and if you dont have the time
and/or inclination to learn this then you would be better of going down
your own path or using the listener removal technique. You may find
that you will reach an impasse with this approach though, and be forced
to learn the 'proper' way.
Lew - 15 Dec 2006 14:07 GMT
> Vector<String> set=new Vector<String>();
Are you relying on the synchronization of Vector methods? If not, a better
declaration might be:
List <String> set = new ArrayList <String>();
Also, since there is a Set type, naming a List variable "set" might be a tad
misleading.
- Lew