how to use chosen item in dropdown list in controlp5

edited February 2016 in Library Questions

Hi everyone,

So I have two dropdown list menus. One of them chooses an activity folder and the second chooses the events in the activity folder. I only want the second dropdown list to show up when the user chooses an activity in the first dropdown menu. I can do this with .hide() and .show(), but the I am not quite sure how to detect when the user has actually chose something. I saw that people use the void controlEvent(){} but this activates for all controllers, so it might get a little funky with two drop down menus(i also have a slider).

Oh yeah one side question: How does changing the label font size work? By default its pretty small and hard to read.

Thanks!

Answers

  • ControlEvent would be the way to go. Use this structure:

    import controlP5.*;
    
    
    DropdownList menu1, menu2;
    ControlP5 cp5;
    
    void setup()
    {
      size(400, 600);
      cp5 = new ControlP5(this);
    
      menu1 = cp5.addDropdownList("menu1")
        .setPosition(10, 100);
      menu1.addItem("New", 0);
      menu1.addItem("Load", 1);
      menu1.addItem("Save", 2);
      menu1.addItem("Save As", 3);
      menu1.addItem("Exit", 4);
      ;
    
      menu2 = cp5.addDropdownList("menu2")
        .setPosition(200, 100);
      menu2.addItem("Play", 0);
      menu2.addItem("Pause", 1);
      menu2.addItem("Stop", 2);
      menu2.addItem("Forward", 3);
      menu2.addItem("Back", 4);
    }
    
    void draw()
    {
      background(127);
    }
    
    void controlEvent(ControlEvent theControlEvent) 
    {
      if (theControlEvent.isFrom(menu1))
      {  
        println("Menu 1");
      }
      if (theControlEvent.getName().equals("menu2"))
      {
        println("Menu 2");
      }
    
      if (theControlEvent.isGroup()) {
        println("event from group : "+theControlEvent.getGroup().getValue()+" from "+theControlEvent.getGroup());
      }
    }
    
  • edited February 2016

    Thanks!!! That's what I needed exactly! Would you happen to know how to change the font size of the dropDownList?

  • You can use cp5.setFont(PFont font) to change the font of all items. Don't forget to change the size of the dropdownlist accordingly...

  • edited February 2016

    Is there a way to target one thing only like men1.setFont(PFont font) instead of all of them? I also have a slider that I want to keep the default font size.

    edit: dropdownlist.addItems() takes in an arraylist of strings as its parameters. However, I need to sort the arrayist by the numeric value. Collections.sort(arraylist) will only sort it like: 1, 123, 2, 222, 3, 4, 53, 6.
    But I would need them sorted like 1, 2, 3, 4, 6, 53, 123, 222. Because of this i stored the values as an arraylist of integers to sort. But now I'm having problems adding the list into the dropwdownlist. Do you have any ideas?

  • That's a hard one as the sorting is using a nonstandard rule. You can try to define your own Comparator (Google will be much better than me at explaining this). If the list is always the same and not very long you're better off sorting it yourself.

Sign In or Register to comment.