-
Bug
-
Resolution: Duplicate
-
P4
-
None
-
1.1.5
-
x86
-
windows_nt
Name: joT67522 Date: 12/15/97
public synchronized void insert(String item, int index) {
if (index < 0) {
throw new IllegalArgumentException("index less than zero.");
}
int nitems = getItemCount();
Vector tempItems = new Vector();
/* Remove the item at index, nitems-index times
storing them in a temporary vector in the
order they appear on the choice menu.
*/
for (int i = index ; i < nitems; i++) {
tempItems.addElement(getItem(index));
remove(index);
}
add(item);
/* Add the removed items back to the choice menu, they
are already in the correct order in the temp vector.
*/
for (int i = 0; i < tempItems.size() ; i++) {
add((String)tempItems.elementAt(i));
}
}
The problem is not actual bug, but it is a very bad implementation. The
code now implemented has bad side effects.
If, for example, you do an insert("Filip",0) on a choice box the code will
first delete all the items in the choice box and after that it will add all
the items again.
This takes a lot of time since it's a peer implementation and will also
result in a visual effect for the user.
Since the Choice peer has a addItem(String,int) I suggest that you should
use that instead since it probably implements a better algorithm.
This code is implemented in the List component and works better and faster
without any visual side effects.
public void synchronized insert(String item, int index) {
if (index < -1 || index >= items.size()) {
index = -1;
}
if (index == -1) {
items.addElement(item);
} else {
items.insertElementAt(item, index);
}
ListPeer peer = (ChoicePeer)this.peer;
if (peer != null) {
peer.addItem(item, index);
}
(Review ID: 21808)
======================================================================
- duplicates
-
JDK-4115130 allow AWT toolkits to implement Choice.insert
-
- Resolved
-