# Dropdownlist from csv file

**URL:** https://discourse.processing.org/t/dropdownlist-from-csv-file/27321
**Category:** Libraries
**Created:** [January 23, 2021, 11:51pm UTC](https://discourse.processing.org/t/dropdownlist-from-csv-file/27321 "2021-01-23T23:51:41Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![Mate\_PL](https://avatars.discourse-cdn.com/v4/letter/m/278dde/32.png) [@Mate\_PL](https://discourse.processing.org/u/Mate_PL)
#### Post date: [January 23, 2021, 11:51pm UTC](https://discourse.processing.org/t/dropdownlist-from-csv-file/27321/1 "2021-01-23T23:51:41Z")

</div>

Hello, I am asking for an example of how to complete the dropdownlist from the controlP5 library from a csv file. The csv file looks like  
Id, slider1, slider2, slider 3  
Settings\_1, 10, 12, 20  
Settings\_2, 12, 20, 30  
…  
I would like the Id field to be dropdownl.addItem “”  
and use the values for the rest of the program.  
I cannot find such an example anywhere. I wanted to use the loadTable and saveTable functions  
Maybe someone would have a code snippet as an example.  
Thank you.

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [January 24, 2021, 9:55am UTC](https://discourse.processing.org/t/dropdownlist-from-csv-file/27321/2 "2021-01-24T09:55:13Z")

</div>

can you show your attempt?

You should at least try it

---

<div class="post-metadata">

### Author: ![Mate\_PL](https://avatars.discourse-cdn.com/v4/letter/m/278dde/32.png) [@Mate\_PL](https://discourse.processing.org/u/Mate_PL)
#### Post date: [January 24, 2021, 10:29am UTC](https://discourse.processing.org/t/dropdownlist-from-csv-file/27321/3 "2021-01-24T10:29:52Z")

</div>

I have a standard procedure taken from the command list + drop down list settings. This is my first GUI program to change the values in an arduino program. Usually I declared it to myself in the program and chose it with the “case” but I wanted to go a step further.

```auto
Table table

void setup() {
  clear();
  size(700, 400 );
  cp5 = new ControlP5(this);
 
 
 table = loadTable("Slider.csv", "header");

  println(table.getRowCount() + " total rows in table");

  for (TableRow row : table.rows()) {

    String id = row.getString("id");
    int slider1 = row.getInt("slider1");
    int slider2 = row.getInt("slider2");
    int slider3 = row.getInt("slider3");

  ddl1 = cp5.addDropdownList("Settings")
          .setPosition(50, 200)
          .setSize(140, 200)
          .setHeight(210)
          .setItemHeight(40)
          .setBarHeight(50)
          .setFont(font)
          .setColorBackground(color(60))
          .setColorActive(color(255, 128))
          ;
 
 
 

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [January 24, 2021, 10:40am UTC](https://discourse.processing.org/t/dropdownlist-from-csv-file/27321/4 "2021-01-24T10:40:48Z")

</div>

somehow the Sketch is not complete but cut off?

please post again

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [January 24, 2021, 10:58am UTC](https://discourse.processing.org/t/dropdownlist-from-csv-file/27321/5 "2021-01-24T10:58:26Z")

</div>

here ONE dropdownlist is filled with items from the csv file

```auto
import controlP5.*;

Table table;
ControlP5 cp5; 

DropdownList ddl1 ; 

void setup() {

  size(700, 400 );
  clear();

  cp5 = new ControlP5(this);

  table = loadTable("Slider.csv", "header");
  println(table.getRowCount() + " total rows in table");

  ddl1 = cp5.addDropdownList("Settings")
    .setPosition(50, 200)
    .setSize(140, 200)
    .setHeight(210)
    .setItemHeight(40)
    .setBarHeight(50)
    // .setFont(font)
    .setColorBackground(color(60))
    .setColorActive(color(255, 128))
    ;

  int i=0; 
  for (TableRow row : table.rows()) {

    String id = row.getString("Id");
    int slider1 = row.getInt("slider1");
    int slider2 = row.getInt("slider2");
    int slider3 = row.getInt("slider3");

    ddl1.addItem(id +" "+i+ " -> ", slider1);
    i++;
    //
  }//for
}//func

void draw() {
  background(128);
}

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [January 24, 2021, 11:06am UTC](https://discourse.processing.org/t/dropdownlist-from-csv-file/27321/6 "2021-01-24T11:06:40Z")

</div>

> [@Mate\_PL](#):
>
> I would like the Id field to be dropdownl.addItem “”  
> and use the values for the rest of the program.

This description is not so clear. Settings\_1 and Settings\_2 are now the captions for the items in the list. But what about slider1, slider2, slider 3?

The csv is wrong: better without spaces after , and before the 3 (in “slider 3”)

```auto
Id,slider1,slider2,slider3
Settings_1, 10, 12, 20
Settings_2, 12, 20, 30
…

```

**Hey, and welcome to the forum!**

**Great to have you here!**

---

<div class="post-metadata">

### Author: ![Mate\_PL](https://avatars.discourse-cdn.com/v4/letter/m/278dde/32.png) [@Mate\_PL](https://discourse.processing.org/u/Mate_PL)
#### Post date: [January 24, 2021, 11:22am UTC](https://discourse.processing.org/t/dropdownlist-from-csv-file/27321/7 "2021-01-24T11:22:29Z")

</div>

Thank you, I will deal with your example. I’m sorry you had to correct such stupid things, but I am a mechanic who has to do some simple programming from time to time (very simple) and ask such silly questions in such a good forum.

Thank you. the csv file is to be expanded and the Slider3 value is to be added. My code:

```auto
import processing.serial.*;
import controlP5.*;
 
ControlP5 cp5;
DropdownList ddl1;
//Slider Range1MaxControl,Range1MinControl,Range2MinControl,Range2MaxControl;
 
Serial myPort;
 
String portName, id;
int serialListIndex;
int k, slider1, slider2;
int Range1Min,Range1Max,Range2Min,Range2Max;
Table table;

void setup() {
  clear();
  size(700, 400 );
  cp5 = new ControlP5(this);

ddl1 = cp5.addDropdownList("Settings")
          .setPosition(50, 200)
          .setSize(140, 200)
          .setHeight(210)
          .setItemHeight(40)
          .setBarHeight(50)
          .setFont(font)
          .setColorBackground(color(60))
          .setColorActive(color(255, 128))
          ;
 
 
 table = loadTable("Slider.csv", "header");
  for (TableRow row : table.rows()) {

    String id = row.getString("id");
    int slider1 = row.getInt("Slider_1");
    int slider2 = row.getInt("Slider_2");
  }
  // Here it is messed up because assigning values from table to array should be in the same for loop.
    for (int i=0; i=table.getRowCount() ; i++) {          
            id[i] =row.getString("id");
            slider1[i]=row.getInt("Slider_1");
            slider2[i]=row.getInt("Slider_2");
            ddl1.addItem(id, i); //add the items in the list
    }

     
                      
}
 
void draw() {
  background(128);
 

        if(theEvent.isController() && ddl1.isMouseOver())
    {
      switch(int(theEvent.getController().getValue()))
      {
    for (int i=0; i=table.getRowCount() ; i++) {          
           
        case [i]:
          Range1Min=(slider1[i]);
          Range1Max=(slider2[i]);
          break;
    }
      }
    
}
}

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [January 24, 2021, 12:00pm UTC](https://discourse.processing.org/t/dropdownlist-from-csv-file/27321/8 "2021-01-24T12:00:00Z")

</div>

remember to click ctrl-t in processing (not in the forum) to get nice **auto-format**

---

<div class="post-metadata">

### Author: ![Mate\_PL](https://avatars.discourse-cdn.com/v4/letter/m/278dde/32.png) [@Mate\_PL](https://discourse.processing.org/u/Mate_PL)
#### Post date: [January 24, 2021, 12:11pm UTC](https://discourse.processing.org/t/dropdownlist-from-csv-file/27321/9 "2021-01-24T12:11:15Z")

</div>

I guess it worked: Maybe someone green like me will need it someday …

```auto
/*slider.csv
 Id,slider1,slider2
 Settings_1,10,12
 Settings_2,40,50
 Settings_3,10,60
 Settings_4,240,150
 Settings_5,120,12
 Settings_6,402,50
 */

import controlP5.*;

Table table;
ControlP5 cp5; 

DropdownList ddl1 ; 
int[] slide1;  
int[] slide2; 
void setup() {

  size(700, 400 );
  clear();

  cp5 = new ControlP5(this);

  table = loadTable("Slider.csv", "header");

  println(table.getRowCount() + " total rows in table");

  slide1= new int[table.getRowCount()];
  slide2= new int[table.getRowCount()];
  ddl1 = cp5.addDropdownList("Settings")
    .setPosition(50, 200)
    .setSize(140, 200)
    .setHeight(210)
    .setItemHeight(40)
    .setBarHeight(50)
    // .setFont(font)
    .setColorBackground(color(60))
    .setColorActive(color(255, 128))
    ;

  int i=0; 

  for (TableRow row : table.rows()) {

    String id = row.getString("Id");
    int slider1 = row.getInt("slider1");
    int slider2 = row.getInt("slider2");

    ddl1.addItem(id +" "+i+ " -> ", slider1);
    slide1[i]=slider1;
    slide2[i]=slider2;

    i++;
    //
  }//for
}//func

void draw() {
  background(128);
  println(slide1[0]);
  println(slide2[4]);
}

```

---

<div class="post-metadata">

### Author: ![Mate\_PL](https://avatars.discourse-cdn.com/v4/letter/m/278dde/32.png) [@Mate\_PL](https://discourse.processing.org/u/Mate_PL)
#### Post date: [January 25, 2021, 9:33pm UTC](https://discourse.processing.org/t/dropdownlist-from-csv-file/27321/10 "2021-01-25T21:33:50Z")

</div>

Helli I would have one more request. I am trying to use this code to read and select a COM port. But with more COM, the list doesn’t scroll. Jumps and I can’t select COM, from the bottom, invisible part of the list. Please help/

```auto
import processing.serial.*;
import controlP5.*;
 
ControlP5 cp5;
DropdownList d1;
 
Serial myPort;
 
String portName;
int serialListIndex;
 
void setup() {
  clear();
  size(700, 400 );
  cp5 = new ControlP5(this);
 
  PFont pfont = createFont("Arial",10,true); //Create a font
  ControlFont font = new ControlFont(pfont,20); //font, font-size
 
  d1 = cp5.addDropdownList("myList-d1")
          .setPosition(100, 100)
          .setSize(100, 200)
          .setHeight(210)
          .setItemHeight(40)
          .setBarHeight(50)
          .setFont(font)
          .setColorBackground(color(60))
          .setColorActive(color(255, 128))
          ;
 
      d1.getCaptionLabel().set("PORT"); //set PORT before anything is selected
 
      portName = Serial.list()[0]; //0 as default
      myPort = new Serial(this, portName, 9600);
}
 
void draw() {
  background(128);
 
  if(d1.isMouseOver()) {
   d1.clear(); //Delete all the items
   for (int i=0;i<Serial.list().length;i++) {
     d1.addItem(Serial.list()[i], i); //add the items in the list
   }
  }
  if ( myPort.available() > 0) { //read incoming data from serial port
    println(myPort.readStringUntil('\n')); //read until new input
   } 
}
 
void controlEvent(ControlEvent theEvent) { //when something in the list is selected
    myPort.clear(); //delete the port
    myPort.stop(); //stop the port
    if (theEvent.isController() && d1.isMouseOver()) {
    portName = Serial.list()[int(theEvent.getController().getValue())]; //port name is set to the selected port in the dropDownMeny
    myPort = new Serial(this, portName, 9600); //Create a new connection
    println("Serial index set to: " + theEvent.getController().getValue());
    delay(2000); 
    }
}

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [January 25, 2021, 10:40pm UTC](https://discourse.processing.org/t/dropdownlist-from-csv-file/27321/11 "2021-01-25T22:40:58Z")

</div>

Apparently I don’t have any ports.

So I just use a String list instead, OK?

Do you see a scrolling here? I do.

```auto

import processing.serial.*;
import controlP5.*;

ControlP5 cp5;
DropdownList d1;

Serial myPort;

String portName;
int serialListIndex;

void setup() {
  clear();
  size(700, 400 );
  cp5 = new ControlP5(this);

  PFont pfont = createFont("Arial", 10, true); //Create a font
  ControlFont font = new ControlFont(pfont, 20); //font, font-size

  d1 = cp5.addDropdownList("myList-d1")
    .setPosition(100, 100)
    .setSize(100, 200)
    .setHeight(210)
    .setItemHeight(40)
    .setBarHeight(50)
    .setFont(font)
    .setColorBackground(color(60))
    .setColorActive(color(255, 128))
    ;

  d1.getCaptionLabel().set("PORT"); //set PORT before anything is selected

  //println(Serial.list().length); 
  // portName = Serial.list()[0]; //0 as default
  //String[] portNameList = Serial.list(); //0 as default
  String[] portNameList = { 
    "dsf", "ed", "df", "urz", "dsf", "ed", "df", "urz", "dsf", "ed", "df", "urz", "dsf", "ed", "df", "urz", "dsf", "ed", "df", "urz", "dsf", "ed", "df", "urz", "dsf", "ed", "df", "urz", "dsf", "ed", "df", "urz", "dsf", "ed", "df", "urz", "dsf", "ed", "df", "urz"
  }; 
  print("here ");
  // println( portName2);

  for (String s1 : portNameList) {
    d1.addItem(s1, "!");
  } 

  //myPort = new Serial(this, portName, 9600);
}

void draw() {
  background(128);
}

void controlEvent(ControlEvent theEvent) { //when something in the list is selected
  myPort.clear(); //delete the port
  myPort.stop(); //stop the port
  if (theEvent.isController() && d1.isMouseOver()) {
    portName = Serial.list()[int(theEvent.getController().getValue())]; //port name is set to the selected port in the dropDownMeny
    myPort = new Serial(this, portName, 9600); //Create a new connection
    println("Serial index set to: " + theEvent.getController().getValue());
    delay(2000);
  }
}
//

```

---

<div class="post-metadata">

### Author: ![Mate\_PL](https://avatars.discourse-cdn.com/v4/letter/m/278dde/32.png) [@Mate\_PL](https://discourse.processing.org/u/Mate_PL)
#### Post date: [February 1, 2021, 4:32pm UTC](https://discourse.processing.org/t/dropdownlist-from-csv-file/27321/12 "2021-02-01T16:32:44Z")

</div>

Sorry to just write back, but I was busy. Thank you for your help. I threw the COM ports section into Setup and it works. I have one more prosaic problem.

```auto
    String myString = myPort.readStringUntil(97); //read until "a"
     println(myString);

```

By COM, I send a text in the form: number + digit, e.g. 150a, I want to read it and convert it into an integer variable. As a String I read 150a, but I can’t convert it to an int.

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [February 2, 2021, 8:05am UTC](https://discourse.processing.org/t/dropdownlist-from-csv-file/27321/13 "2021-02-02T08:05:58Z")

</div>

You can use `substring` command to cut the last letter off

Then use `int()` command

```auto

String old = "150a";

String new1 = old.substring(0, old.length()-1); 

println (new1); 

int newInt = int(new1); 

println (newInt);

```

---

<div class="post-metadata">

### Author: ![Mate\_PL](https://avatars.discourse-cdn.com/v4/letter/m/278dde/32.png) [@Mate\_PL](https://discourse.processing.org/u/Mate_PL)
#### Post date: [February 2, 2021, 10:31pm UTC](https://discourse.processing.org/t/dropdownlist-from-csv-file/27321/14 "2021-02-02T22:31:16Z")

</div>

Thanks, works perfect !!!
