Remove all items from G4P Droplist

Hi, I’m trying to dynamically empty a drop list on certain conditions. I notice there is no “length” property for the droplist.

I don’t know how many items I will have, so when I try to use the “removeItem” method, it doesn’t work.

Any thoughts on how to totally empty a list so I can repopulate it with new values?

thanks,

Mike

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);
2 Likes

Thank you Peter, that works.

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.

all my best,

Mike

Just stumbled on this item while searching for a solution. For anyone interested, here’s my simple code.

@Quark, feel free to burn is down :-1:

See code below….


void ClearDroplist()
{

Boolean EndOfList = false;

while (EndOfList != true)
{
if (drplst.removeItem(0)==false) EndOfList = true;
}

}


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 :innocent: :grin: :partying_face:

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);
}
2 Likes

Hi Quark, Always up for a good solution. :smiley:

Your version is absolute more clean. While reading your sample code I, again, learned something new. :cat_with_wry_smile:

Thanks for your suggestions.

Cheers, Marcel.