How to get dropdownlist item name

edited February 2014 in Library Questions

I am using controlP5 library for creating GUI for my app. I have dropdownist and I want to add/remove items, set names of the items - i found solutions in examples. But I can't find how to get name of the choosen item (not its value!).

I tried this solution but this is not what i actually want because this solution implies item name corresponding to its value (i don't want to use value of item, I need its name to use as name of file).

Answers

  • Answer ✓

    It's not clear if you just want to get the name of the item as such or if you want to get the name of the item sending the event. Since you mention "chosen item" I assume it's the latter. However, for completeness here is an example of both:

    Code Example

    import controlP5.*;
    ControlP5 cp5;
    
    void setup() {
      size(300, 300);
      cp5 = new ControlP5(this);
      DropdownList d1 = cp5.addDropdownList("myList-d1").setPosition(75, 50);
      for (int i=0; i<7; i++) {
        d1.addItem("item "+i, i);
      }
    
      // get name of item
      ListBoxItem lbi = d1.getItem(3);
      println("My name is: " + lbi.getName());
    }
    
    void draw() {
      background(128);
    }
    
    void controlEvent(ControlEvent theEvent) {
      if (theEvent.isGroup()) {
        // get name of item sending an event
        println("My name is: " + theEvent.getGroup().getCaptionLabel().getText());
      }
    }
    
  • Thanks a lot for quick answer, it works perfectly!

Sign In or Register to comment.