ControlP5 ListBox problem in Processing V3

edited February 2017 in Library Questions

Hello

I'm new to Processing and I am using V3.2.4. I need to display a ListBox from the ControlP5 library with several text entries in the list and depending on which list item is selected I need to call other functions.

The code in a previous post below gets me close to what I need however when I paste the code into a new sketch it errors in a few places e.g.

The function captionLabel() does not exist

The function valueLabel() does not exist

The method group() from the type ControlEvent is deprecated

The method name() from the type ControlEvent is deprecated

I am aware that older code will not work in more recent versions of Processing and library functions change slightly e.g. captionLabel() becomes getCaptionLabel()

In this code the void draw()function does work however the ControlEvent() does not return anything in the println statement. This means that I cannot identify the item selected from the items in the listbox and therefore cannot move into other functions/actions.

 void controlEvent(ControlEvent theEvent) {
      if (theEvent.isGroup()) {
        // an event from a group e.g. scrollList
        println(theEvent.group().value()+" from "+theEvent.name());
        if (theEvent.name() == "myList") {  // if we got an event from myList
          println("choosed " + employee[int(theEvent.group().value())]); // look through the array
        }
      }
    }

I have made several unsuccessful attempts to get this to work in V3.2.4 and have run out of ideas. Any advice on what needs to change in the code will be greatly appreciated thanks.

This is the url to the forum post:

https://forum.processing.org/one/topic/how-to-get-controlp5-library-listbox-item-name.html

This is the code:

import controlP5.*;

ControlP5 controlP5;
//set the names here
String[] employee = {
  "empA", "empB", "empC", "empD", "empE", "empF"
};

ListBox l;
int cnt = 0;
void setup() {
  size(400, 400);
  frameRate(30);
  controlP5 = new ControlP5(this);
  l = controlP5.addListBox("myList", 100, 100, 120, 120);
  l.setItemHeight(15);
  l.setBarHeight(15);

  l.captionLabel().toUpperCase(true);
  l.captionLabel().set("something else");
  l.captionLabel().style().marginTop = 3;
  l.valueLabel().style().marginTop = 3; // the +/- sign
  //l.setBackgroundColor(color(100,0,0));

  for (int i=0;i<employee.length;i++) {
    l.addItem(employee[i], i);
  }
  l.setColorBackground(color(255, 128));
  l.setColorActive(color(0, 0, 255, 128));
}

void controlEvent(ControlEvent theEvent) {
  if (theEvent.isGroup()) {
    // an event from a group e.g. scrollList
    println(theEvent.group().value()+" from "+theEvent.name());
    if (theEvent.name() == "myList") {  // if we got an event from myList
      println("choosed " + employee[int(theEvent.group().value())]); // look through the array
    }
  }
}

void draw() {
  background(128);
  // scroll the scroll List according to the mouseX position
  // when holding down SPACE.
  if (keyPressed && key==' ') {
    //l.scroll(mouseX/((float)width)); // scroll taks values between 0 and 1
  }
  if (keyPressed && key==' ') {
    l.setWidth(mouseX);
  }
}

Answers

  • captionLable() doesn't exist in the documentation. Check the reference for ControlP5: http://www.sojamo.de/libraries/controlP5/examples/controllers/ControlP5listBox/ControlP5listBox.pde

    However, you have other functions close to your call: setCaptionLabel(String) and getCaptionLabel().

    Kf

  • edited February 2017

    @kfrajer - thank you for your reply, but with respect, that isn't the question I was asking.

    I am aware that many of the functions in that code do not exist anymore.

    Basically, what I was trying to say is the code in the void controlEvent(ControlEvent theEvent) function does not do what it seems it should do. My question is - what code should I use to call functions when a certain item in the list has been chosen?

  • I didn't read all your initial post. I was trying to run your code and I got those errors. Hence, my answer to your post. In general, you want to run the examples provided by the library. If the examples don't work, i will assume there are compatibility issues with the library. I will focus in getting the examples running first. I remember seeing a post talking about a new version of controlP5. I will have to check previous posts to see if I can find it.

    Kf

  • Ok, so looking at the example provided in ListBox, right at the top in the comment section it says:

    DEPRECATED, use ScrollableList instead.

    Good luck,

    Kf

  • @kfrajer - I see.

    I have worked out that changing captionLabel to getCaptionlabel(), valueLabel() to getValueLabel(), group() to getGroup(), name() to getName()

    and so on.. leaves me with no errors and the program runs. But I still don't know how to call functions when a certain item in the list has been chosen? I have a feeling it is such a easy snippet of code but I cant seem to get it right.

Sign In or Register to comment.