Loading...
Logo
Processing Forum
Hi, I'm a noob to these forums and to programming, so I hope I'm asking this in the right place. Anyways, I'm trying to use ControlP5's dropdown list, but I can't figure out where I actually make stuff happen when I click on the items.

Here's the code I have; basically, when I click on an item, I want it to load an image, and if another item in the same menu is clicked, it will change the image to a different one.

  /**
 * ControlP5dropdownList
 * A dropdownList controller, based on the ListBox controller.
 * the currently selected dropdownlist item is displayed inside
 * the menu bar of the DropdownList.
 
 * by andreas schlegel, 2010
 * www.sojamo.de/libraries/controlP5
 */

import controlP5.*;

ControlP5 controlP5;

DropdownList p1, p2;


//setting height, etc is a mess and spoiled. REPAIR


int cnt = 0;
void setup() {
  size(400,400);
  frameRate(30);
  controlP5 = new ControlP5(this);
  p1 = controlP5.addDropdownList("myList-p1",100,100,100,120);
  customize(p1);
  p2 = controlP5.addDropdownList("myList-p2",220,100,100,120);
  customize(p2);
}

void customize(DropdownList ddl) {
  ddl.setBackgroundColor(color(190));
  ddl.setItemHeight(20);
  ddl.setBarHeight(15);
  ddl.captionLabel().set("pulldown");
  ddl.captionLabel().style().marginTop = 3;
  ddl.captionLabel().style().marginLeft = 3;
  ddl.valueLabel().style().marginTop = 3;
   ddl.addItem("Accents",1);
   ddl.addItem("Peacock Spots",2);
  ddl.setColorBackground(color(60));
  ddl.setColorActive(color(255,128));
}

void keyPressed() {
  if(key=='1') {
    // set the height of a pulldown menu, should always be a multiple of itemHeight
    p1.setHeight(210);
  }
  else if(key=='2') {
    // set the height of a pulldown menu, should always be a multiple of itemHeight
    p1.setHeight(120);
  }
  else if(key=='i') {
    // set the height of a pulldown menu item, should always be a fraction of the pulldown menu
    p1.setItemHeight(30);
  }
  else if(key=='u') {
    // set the height of a pulldown menu item, should always be a fraction of the pulldown menu
    p1.setItemHeight(10);
    p1.setBackgroundColor(color(100,0,0));
  }
  else if(key=='a') {
    // add new items to the pulldown menu
    int n = (int)(random(100000));
    p1.addItem("item "+n, n);
  }
  else if(key=='d') {
    // remove items from the pulldown menu  by name
    p1.removeItem("item "+cnt);
    cnt++;
  }
  else if(key=='c') {
    p1.clear();
  }
}

void controlEvent(ControlEvent theEvent) {
  // PulldownMenu is if type ControlGroup.
  // A controlEvent will be triggered from within the ControlGroup.
  // therefore you need to check the originator of the Event with
  // if (theEvent.isGroup())
  // to avoid an error message from controlP5.

  if (theEvent.isGroup()) {
    // check if the Event was triggered from a ControlGroup
    println(theEvent.group().value()+" from "+theEvent.group());
  } else if(theEvent.isController()) {
    println(theEvent.controller().value()+" from "+theEvent.controller());
  }
}

void draw() {
  background(128);
}


Sorry if this is a stupid question, I'm really new to this.

Replies(2)

The action happens in the controlEvent function. This function listens to what's going on and acts upon it. Check out the example below. You will need to put two images called image1.jpg and image2.jpg (jpg not jpeg) in the data subdirectory of your sketch to run the code below.

Copy code
  1. // controlP5 stuff
  2. import controlP5.*;
  3. ControlP5 controlP5;
  4. DropdownList p1;

  5. // define an array of images and the variable to select between them
  6. PImage[] images = new PImage[2]; // array of images
  7. int selectedImage; // the variable used to select the image to show on screen

  8. void setup() {
  9.   size(400,400);

  10.   // create the controlP5dropdownlist and give it the name you will use later, in this case ImageSelect
  11.   controlP5 = new ControlP5(this);
  12.   p1 = controlP5.addDropdownList("ImageSelect",width/2-50,100,100,120);
  13.   customize(p1);

  14.   // load images in setup
  15.   images[0] = loadImage("image1.jpg"); // note: arrays start at zero!
  16.   images[1] = loadImage("image2.jpg");
  17. }

  18. void draw() {
  19.   background(128);
  20.   image(images[selectedImage],0,0); // show the selected image
  21. }

  22. void customize(DropdownList ddl) {
  23.   ddl.setBackgroundColor(color(190));
  24.   ddl.setItemHeight(20);
  25.   ddl.setBarHeight(15);
  26.   ddl.captionLabel().set("Pulldown");
  27.   ddl.captionLabel().style().marginTop = 3;
  28.   ddl.captionLabel().style().marginLeft = 3;
  29.   ddl.valueLabel().style().marginTop = 3;
  30.   ddl.addItem("Image One",0); // give each item a value, in this example starting from zero
  31.   ddl.addItem("Image Two",1);
  32.   ddl.setColorBackground(color(60));
  33.   ddl.setColorActive(color(255,128));
  34. }

  35. // this functions listens to events and can act upon it
  36. void controlEvent(ControlEvent theEvent) {
  37.   // if the event is from a group, which is the case with the dropdownlist
  38.   if (theEvent.isGroup()) {
  39.     // if the name of the event is equal to ImageSelect (aka the name of our dropdownlist)
  40.     if (theEvent.group().name() == "ImageSelect") {
  41.       // then do stuff, in this case: set the variable selectedImage to the value associated
  42.       // with the item from the dropdownlist (which in this case is either 0 or 1)
  43.       selectedImage = int(theEvent.group().value());
  44.     }
  45.   } else if(theEvent.isController()) {
  46.     // not used in this sketch, but has to be included
  47.   }
  48. }

For my project I needed the name of the selected item instead of the value. Maybe this is helpful for someone else. 

Copy code
  1. import controlP5.*;

  2. ControlP5 controlP5;
  3. DropdownList dropdown;

  4. int cnt = 0;
  5. void setup() {
  6.   size(400,400);
  7.   frameRate(30);
  8.   controlP5 = new ControlP5(this);
  9.   dropdown = controlP5.addDropdownList("aDropdownList",100,100,100,120);
  10.   customize(dropdown);
  11. }

  12. void customize(DropdownList ddl) {
  13.   ddl.setBackgroundColor(color(190));
  14.   ddl.setItemHeight(20);
  15.   ddl.setBarHeight(15);
  16.   ddl.captionLabel().set("pulldown");
  17.   ddl.captionLabel().style().marginTop = 3;
  18.   ddl.captionLabel().style().marginLeft = 3;
  19.   ddl.valueLabel().style().marginTop = 3;
  20.   for(int i=0;i<80;i++) {
  21.     ddl.addItem("item "+i,i);
  22.   }
  23.   ddl.setColorBackground(color(60));
  24.   ddl.setColorActive(color(255,128));
  25. }


  26. void controlEvent(ControlEvent theEvent) {
  27.   if (theEvent.isGroup()) {
  28.     // check if the Event was triggered from a ControlGroup
  29.     println(theEvent.group().value()+" from "+theEvent.group());
  30.     if(theEvent.group().name().equals("aDropdownList")){
  31.       ListBoxItem selectedItem = dropdown.getItem((int)theEvent.value());
  32.       if(selectedItem.getName().equals("item 3")){
  33.         println("item 3 clicked... do something here!");
  34.       }
  35.       else if(selectedItem.getName().equals("item 4")){
  36.         println("item 4 clicked... do something here!");
  37.       }
  38.     }
  39.   } else if(theEvent.isController()) {
  40.     println(theEvent.controller().value()+" from "+theEvent.controller()); 
  41.   }
  42. }

  43. void draw() {
  44.   background(128);
  45. }