# Scrollablelist set Item selected from code

**URL:** https://discourse.processing.org/t/scrollablelist-set-item-selected-from-code/2617
**Category:** Libraries
**Created:** [August 13, 2018, 6:26pm UTC](https://discourse.processing.org/t/scrollablelist-set-item-selected-from-code/2617 "2018-08-13T18:26:02Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Dennis](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/dennis/32/1111_2.png) [@Dennis](https://discourse.processing.org/u/Dennis)
#### Post date: [August 13, 2018, 6:26pm UTC](https://discourse.processing.org/t/scrollablelist-set-item-selected-from-code/2617/1 "2018-08-13T18:26:02Z")

</div>

Hello everyone! i’m a new on processing forum.  
I make a Scrolablelist from ControlP5 library and I would like to change via code the selected item:

```auto
import controlP5.*;
import java.util.*;

ControlP5 cp5;

void setup() {
  size(400, 400);
  cp5 = new ControlP5(this);
  List l = Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h");
  /* add a ScrollableList, by default it behaves like a DropdownList */
  cp5.addScrollableList("dropdown")
     .setPosition(100, 100)
     .setSize(200, 100)
     .setBarHeight(20)
     .setItemHeight(20)
     .addItems(l)
     .setType(ScrollableList.LIST) // currently supported DROPDOWN and LIST
     ;
}

```

my intention is something similar to:

```auto
cp5.setItemSelected(2);

```

as if I had manually selected the second item.

I thank you in advance who can help me,  
regards!

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [August 13, 2018, 6:57pm UTC](https://discourse.processing.org/t/scrollablelist-set-item-selected-from-code/2617/2 "2018-08-13T18:57:42Z")

</div>

Hi,  
You can check out the doc. There are the list of all available functions: [http://www.sojamo.de/libraries/controlP5/reference/index.html](http://www.sojamo.de/libraries/controlP5/reference/index.html)

---

<div class="post-metadata">

### Author: ![Dennis](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/dennis/32/1111_2.png) [@Dennis](https://discourse.processing.org/u/Dennis)
#### Post date: [August 13, 2018, 7:04pm UTC](https://discourse.processing.org/t/scrollablelist-set-item-selected-from-code/2617/3 "2018-08-13T19:04:42Z")

</div>

Thanks! I saw that there is the method _setValue_ i try now how does it work!

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [August 13, 2018, 7:10pm UTC](https://discourse.processing.org/t/scrollablelist-set-item-selected-from-code/2617/4 "2018-08-13T19:10:06Z")

</div>

You also have the setItem() one if you know the name of the item and if I’m not mistaken.

---

<div class="post-metadata">

### Author: ![Dennis](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/dennis/32/1111_2.png) [@Dennis](https://discourse.processing.org/u/Dennis)
#### Post date: [August 13, 2018, 7:24pm UTC](https://discourse.processing.org/t/scrollablelist-set-item-selected-from-code/2617/5 "2018-08-13T19:24:31Z")

</div>

Excuse my ignorance but i can’t find the method, isn’t in a list methods that appear with autocomplete code. There another way to use this methods?

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [August 13, 2018, 7:34pm UTC](https://discourse.processing.org/t/scrollablelist-set-item-selected-from-code/2617/6 "2018-08-13T19:34:23Z")

</div>

Here they are:

```auto
setItems(java.util.List<java.lang.String> theItems) 
setItems(java.util.Map<java.lang.String,java.lang.Object> theItems) |
setItems(java.lang.String[] theItems)|

```

I never tried them though so it is really just a guess based on there name.

I’m currently using the `setValue()` function in one of my sketch and it works like a charm 🙂

---

<div class="post-metadata">

### Author: ![Dennis](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/dennis/32/1111_2.png) [@Dennis](https://discourse.processing.org/u/Dennis)
#### Post date: [August 13, 2018, 7:45pm UTC](https://discourse.processing.org/t/scrollablelist-set-item-selected-from-code/2617/7 "2018-08-13T19:45:03Z")

</div>

Thank you very much! i try it!

---

<div class="post-metadata">

### Author: ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)
#### Post date: [August 13, 2018, 8:27pm UTC](https://discourse.processing.org/t/scrollablelist-set-item-selected-from-code/2617/8 "2018-08-13T20:27:06Z")

</div>

I try but not sure if it is possible. Add the following to cp5:

```java
.setValue(1)
.open()

```

This sets the value display in the list when the list is close. Calling open forces to open the list. However, selecting it will be a bit trickier. Selection is tight to mouse events. You might need to override them. If this is your intention, you can look on previous posts related to overriding mouse events with controlP5 and see if you can find something that could use and try in your case.

Kf

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [September 21, 2018, 11:45am UTC](https://discourse.processing.org/t/scrollablelist-set-item-selected-from-code/2617/10 "2018-09-21T11:45:42Z")

</div>

```auto
    articoloCliente = listFileNames(pathDataSet + slash + clienti[clienteSelezionato]); // read a list of Files
    cp5.get(ScrollableList.class, "ARTICOLI").setLabel("ARTICOLI"); // reset the top label 
    cp5.get(ScrollableList.class, "ARTICOLI").setItems(articoloCliente); // change present list with <articoloCliente>
    cp5.get(ScrollableList.class, "ARTICOLI").open(); // force <ScrollableList> open

```

---

<div class="post-metadata">

### Author: ![Dennis](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/dennis/32/1111_2.png) [@Dennis](https://discourse.processing.org/u/Dennis)
#### Post date: [October 8, 2018, 2:06pm UTC](https://discourse.processing.org/t/scrollablelist-set-item-selected-from-code/2617/11 "2018-10-08T14:06:54Z")

</div>

```auto
.setValue(1)
.open()

```

Solved! this solution works, but it should not be used inside of eventHandler because rise nullPointer exception.  
Thanks kfrajer!
