We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOther Libraries › controlP5 - scrollList controll event handling
Page Index Toggle Pages: 1
controlP5 - scrollList controll event handling (Read 632 times)
controlP5 - scrollList controll event handling
Apr 5th, 2009, 5:51am
 
Hej,
i have a problem handling the control events/values of a scrollList correctly. I want to use scrollList with a fixed id an allocate the selected values to a public variable (as it works for a single buttom event).

As i do it i get:
"ERROR. an error occured while forwarding a Controller value
to a method in your program. please check your code for any
possible errors that might occur in this method .
e.g. check for casting errors, possible nullpointers, array overflows ... .
method: controlEvent
exception:  java.lang.reflect.InvocationTargetException"

example code:

import controlP5.*;
ControlP5 controlP5;

public int a;  //controller value from button
public int b;  //controller value from list

void setup() {
 size(400,400);
 frameRate(30);
 controlP5 = new ControlP5(this);
 
 controlP5.addNumberbox("myButton",a,100,160,100,14).setId(1);
 ScrollList l = controlP5.addScrollList("myList",100,100,120,280);
 

 l.setLabel("List");
 for(int i=0;i<4;i++) {
   controlP5.Button b = l.addItem("a"+i,i);
   b.setId(2);                                       // fixed id 2 for scroll list
 }
}

void draw() {
 background(0);
 
 //println("controller value from button " + a);
 //println("controller value from list " + b);
}

void controlEvent(ControlEvent theEvent) {
 switch(theEvent.controller().id()) {
   case(1):
   /*events from my button*/
   a = (int)(theEvent.controller().value());
   break;
   case(2):
   /* events from myList */
   b = (int)(theEvent.controller().value());
   break;  
 }
}

void myList(int theValue) {
 println("123");
}
Re: controlP5 - scrollList controll event handling
Reply #1 - Apr 5th, 2009, 11:26pm
 
hi appolo,
you get this error message for the following reason. a ControlEvent can come from a Controller and from a ControlGroup, too. in the case of a scrollList, the event comes first from the ScrollList as a Group event and then from the Button that belongs to that scrollList. the error occurs at switch(theEvent.controller().id()) { when theEvent.controller() is null and theEvent.group() would hold the source of the event.

a workaround in your case would be:
Code:

void controlEvent(ControlEvent theEvent) {

 if(theEvent.isGroup()) {
 if(theEvent.name().equals("myList")) {
   b = (int)(theEvent.group().value());
   println(b +" / "+theEvent.group().id());
 }
 }
 
 if(theEvent.isController()) {
   switch(theEvent.controller().id()) {
     case(1):
     /*events from my button*/
     a = (int)(theEvent.controller().value());
     break;
     case(2):
     /* events from a controller from myList */
     b = (int)(theEvent.controller().value());
     break;  
   }
 }
}

hope this helps,
andreas
Re: controlP5 - scrollList controll event handling
Reply #2 - Apr 6th, 2009, 3:23am
 
Ahhhh ok! Thanks for the explanation -i was not aware of that there are two control events in this case. Thanks for your fast help - the workaround is perfect!
Greets apollo
Page Index Toggle Pages: 1