import controlP5.*;
ControlP5 cp5;
DropdownList d1, d2;int cnt =0;
void setup(){size(700,400);
cp5 =newControlP5(this);
d1 = cp5.addDropdownList("myList-d1").setPosition(100,100);customize(d1);// customize the first list单独设置一个功能函数// create a second DropdownList
d2 = cp5.addDropdownList("myList-d2").setPosition(400,100).setSize(200,200);customize(d2);// customize the second list}
void customize(DropdownList ddl){// a convenience function to customize a DropdownList
ddl.setBackgroundColor(color(190));
ddl.setItemHeight(20);
ddl.setBarHeight(15);
ddl.getCaptionLabel().set("dropdown");for(int i=0;i<40;i++){
ddl.addItem("item "+i, i);}//ddl.scroll(0);
ddl.setColorBackground(color(60));
ddl.setColorActive(color(255,128));}
void keyPressed(){// some key events to change the properties of DropdownList d1if(key=='1'){// set the height of a pulldown menu, should always be a multiple of itemHeight
d1.setHeight(210);}elseif(key=='2'){// set the height of a pulldown menu, should always be a multiple of itemHeight
d1.setHeight(120);}elseif(key=='3'){// set the height of a pulldown menu item, should always be a fraction of the pulldown menu
d1.setItemHeight(30);}elseif(key=='4'){// set the height of a pulldown menu item, should always be a fraction of the pulldown menu
d1.setItemHeight(12);
d1.setBackgroundColor(color(255));}elseif(key=='5'){// add new items to the pulldown menuint n =(int)(random(100000));
d1.addItem("item "+n, n);}elseif(key=='6'){// remove items from the pulldown menu by name
d1.removeItem("item "+cnt);
cnt++;}elseif(key=='7'){
d1.clear();}}
void controlEvent(ControlEvent theEvent){// DropdownList is of type ControlGroup.// A controlEvent will be triggered from inside the ControlGroup class.// therefore you need to check the originator of the Event with// if (theEvent.isGroup())// to avoid an error message thrown by controlP5.if(theEvent.isGroup()){// check if the Event was triggered from a ControlGroupprintln("event from group : "+theEvent.getGroup().getValue()+" from "+theEvent.getGroup());}elseif(theEvent.isController()){println("event from controller : "+theEvent.getController().getValue()+" from "+theEvent.getController());//group和controller不一样,这个里面点击后都是controller}}
void draw(){background(128);}