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