# Remove all items from G4P Droplist

**URL:** https://discourse.processing.org/t/remove-all-items-from-g4p-droplist/44306
**Category:** Libraries
**Created:** [April 17, 2024, 6:22pm UTC](https://discourse.processing.org/t/remove-all-items-from-g4p-droplist/44306 "2024-04-17T18:22:21Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Thundercat](https://avatars.discourse-cdn.com/v4/letter/t/dbc845/32.png) [@Thundercat](https://discourse.processing.org/u/Thundercat)
#### Post date: [April 17, 2024, 6:22pm UTC](https://discourse.processing.org/t/remove-all-items-from-g4p-droplist/44306/1 "2024-04-17T18:22:21Z")

</div>

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

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [April 18, 2024, 8:18am UTC](https://discourse.processing.org/t/remove-all-items-from-g4p-droplist/44306/2 "2024-04-18T08:18:25Z")

</div>

> [@Thundercat](#):
>
> Any thoughts on how to totally empty a list so I can repopulate it with new values?

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.

```auto
// dlst = a droplist control
// items = an array or list of Strings
// index = the index number for the selected item
dlst.setItems(items, index);

```

---

<div class="post-metadata">

### Author: ![Thundercat](https://avatars.discourse-cdn.com/v4/letter/t/dbc845/32.png) [@Thundercat](https://discourse.processing.org/u/Thundercat)
#### Post date: [April 18, 2024, 11:46am UTC](https://discourse.processing.org/t/remove-all-items-from-g4p-droplist/44306/3 "2024-04-18T11:46:56Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Marcel](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@Marcel](https://discourse.processing.org/u/Marcel)
#### Post date: [October 26, 2025, 7:05pm UTC](https://discourse.processing.org/t/remove-all-items-from-g4p-droplist/44306/4 "2025-10-26T19:05:17Z")

</div>

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 👎

See code below….

* * *

void ClearDroplist()  
{

Boolean EndOfList = false;

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

}

* * *

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [October 26, 2025, 9:54pm UTC](https://discourse.processing.org/t/remove-all-items-from-g4p-droplist/44306/5 "2025-10-26T21:54:30Z")

</div>

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

```auto
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 😇 😁 🥳

```auto
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.

```auto
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);
}

```

---

<div class="post-metadata">

### Author: ![Marcel](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@Marcel](https://discourse.processing.org/u/Marcel)
#### Post date: [October 27, 2025, 6:47am UTC](https://discourse.processing.org/t/remove-all-items-from-g4p-droplist/44306/6 "2025-10-27T06:47:40Z")

</div>

Hi Quark, Always up for a good solution. 😃

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

Thanks for your suggestions.

Cheers, Marcel.
