The droplist control does not like to be empty. The best thing is not to empty the control but simply replace the existing items with a new set of items.
// dlst = a droplist control
// items = an array or list of Strings
// index = the index number for the selected item
dlst.setItems(items, index);
I was struggling with the .removeItem() command. It wasn’t working at all and I couldn’t figure out why. I needed to take a list of 4 things and make it a list of 2 things. Iterating over a randomly high number like 10 (won’t ever go above 10) to make sure I got rid of everything also just wasn’t working.
Not sure what happened, but now I am able to remove items, and just manipulate the first item to be the first item of the newly intended list, and of course your .addItem() works great.
Thank you kindly Peter; you always provide fabulous support.
Its been a long time since I looked at the G4P source code but I decided to have another look before trying out your code.
I have (re)discovered that
There are no public / protected / private methods that can completely empty the droplist control of all items
The smallest number of items permitted is 1
the removeItem(...) method only works if the list is of length 2 or more
I designed and wrote the code for the droplist to always have least one item in the list
So having said that I decided to try your code out and as expected (based on the removeItem() specification) it removed all items except the last one .
I have formatted your code so we have
void marcelClearDroplist() {
Boolean EndOfList = false;
while (EndOfList != true) {
if (dropList1.removeItem(0)==false) EndOfList = true;
}
}
So although your code works I would like to present my version which performs exactly the same function
void quarkClearDroplist() {
while (dropList1.removeItem(0));
}
The sketch below allows you to try out both, the m and q kets allow you to select the clear method and the r key resets the droplist to its original state.
import g4p_controls.*;
String[] list;
int dpsize = 10;
GDropList dropList1;
public void setup() {
size(480, 320, JAVA2D);
list = new String[10];
for (int i = 0; i < list.length; i++)
list[i] = "List item " + (i+1);
createGUI();
}
public void draw() {
background(230);
}
void keyTyped() {
switch (key) {
case 'm':
marcelClearDroplist();
break;
case 'q':
quarkClearDroplist();
break;
case 'r': // Reset droplist to original state
dropList1.setItems(list, 0);
break;
}
}
// Remove all items but the last one
void marcelClearDroplist() {
Boolean EndOfList = false;
while (EndOfList != true) {
if (dropList1.removeItem(0)==false) EndOfList = true;
}
}
// Remove all items but the last one
void quarkClearDroplist() {
while (dropList1.removeItem(0));
}
public void createGUI() {
G4P.messagesEnabled(false);
G4P.setGlobalColorScheme(GCScheme.BLUE_SCHEME);
G4P.setMouseOverEnabled(false);
surface.setTitle("Clear droplist demo");
dropList1 = new GDropList(this, 103, 38, 244, 185, 4, 10);
dropList1.setItems(list, 0);
}