How to read selected field in scrollableList?

Disclaimer: I have no prior Java experience and very little coding experience in general.

I’m trying to write a simple GUI that takes user input from a dropdown and depending on that performs simple data analysis on sensor values that I read from an arduino. Here’s the code:


import processing.serial.*;
import controlP5.*;
import java.util.*;

ControlP5 cp5;
Serial myPort;
PrintWriter output;

String val;
int i=0;
int lf = 10;
int tempc;
int lightint;

IntList datal;
IntList datat;

int count  = 0;
//int count = 0;

void setup()
{
    size(500, 500);
    
    datal = new IntList();
    datat = new IntList();
    
    cp5 = new ControlP5(this);
  
    List l = Arrays.asList("1-2", "2-3", "3-4", "4-5", "5-6", "6-7", "7-8", "8-9", "9-10", "10-11", "11-12");
    /* add a ScrollableList, by default it behaves like a DropdownList */
    cp5.addScrollableList("Time Slot")
        .setPosition(75, 240)
        .setSize(75, 100)
        .setBarHeight(20)
        .setItemHeight(20)
        .addItems(l)
        .setOpen(false)
        // .setType(ScrollableList.LIST) // currently supported DROPDOWN and LIST
        ;
    //cp5.addScrollableList("END TIME")
    //    .setPosition(width-150, 240)
    //    .setSize(75, 100)
    //    .setBarHeight(20)
    //    .setItemHeight(20)
    //    .addItems(l)
    //    .setOpen(false)
    //    // .setType(ScrollableList.LIST) // currently supported DROPDOWN and LIST
    //    ;
        
    println(Serial.list());
    String portName = Serial.list()[0];
    myPort = new Serial(this, portName, 9600);
    myPort.bufferUntil(lf);
    
    output = createWriter("data.csv");
}

void draw()
{                 
    background(255);
    
    //current time
    int s = second();  // Values from 0 - 59
    int m = minute();  // Values from 0 - 59
    int h = hour();    // Values from 0 - 23
    text(h, 400, 15);
    text(":", 410, 15);
    text(m, 430, 15);
    text(":", 440, 15);
    text(s, 460, 15);
    
    //title
    fill(0);
    textAlign(CENTER, TOP);
    textSize(25);
    text("ESD PROJECT 1", width/2, 30);
    
    //Real Time Data
    textAlign(LEFT, TOP);
    textSize(20);
    text("Real time data", 50, 80);
    textSize(16);
    text("Temperature:", 75, 120);
    text("Light:", 75, 140);
    text(tempc, 300, 120);
    text(lightint, 300, 140);
 
    //Averaged data
    textSize(20);
    text("Averaged data", 50, 180);
    textSize(16);
    text("Choose Start Time", 75, 210);
    textAlign(RIGHT, TOP);
    text("Choose End Time", width-75, 210);
    
    if (count > 10 && count < 12)
    {
        output.println(h + "," + m + "," + s + "," + tempc + "," + lightint); 
        if (count == 12)
            count = 0;
    }
    count++;
}

void serialEvent(Serial myPort)
{
    val = myPort.readStringUntil(lf);
    // if you got any bytes other than the linefeed:
    if (val != null) 
    {
        val = trim(val);
        
        int sensorData[] = int(split(val,','));
        
        lightint = sensorData[0];
        datal.append(sensorData[0]);
        println(datal);
        tempc = sensorData[1];
        datat.append(sensorData[1]);
        println(datat);
    }
}

void keyPressed() 
{
    output.flush(); // Writes the remaining data to the file
    output.close(); // Finishes the file
    exit(); // Stops the program
}

I’m unable to figure out as to how to retrieve the field that the user chooses from the dropdown. Any help. And if someone would be so kind to go through the rest of the code and point out any mistakes, I’d be glad.

Thanks!

1 Like

Hi iamthehype,

I advise you to go take a look at the java doc of the controlP5 library, you can find plenty of useful informations.

The code below should help you understand how to add an event listener to the dropdown list and how to retrieve the value:

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");

  cp5.addScrollableList("dropdown")
    .setPosition(100, 100)
    .setSize(200, 100)
    .setBarHeight(20)
    .setItemHeight(20)
    .addItems(l);

  cp5.getController("dropdown").getCaptionLabel().setColor(color(255, 0, 0));
  cp5.getController("dropdown").getCaptionLabel().alignX(ControlP5.RIGHT);

  cp5.getController("dropdown").addCallback(new CallbackListener() {
    public void controlEvent(CallbackEvent theEvent) {
      switch(theEvent.getAction()) {
        case(ControlP5.ACTION_RELEASE): 
        println(cp5.getController("dropdown").getLabel());
      }
    }
  }
  );
}

void draw() {
  background(240);
}

You can also use the G4P library. I personally found it a bit easier to begin with.

2 Likes

Thanks for replying. I did come across the javadocs but had a hard time understanding them. Thanks for the g4p suggestion. I’ll try both and see which works best.

Please explore this post: Scrollable list to Bang

Kf

2 Likes