List specific string elements within ControlP5 drop down menu

edited July 2014 in Library Questions

Hey all,

so ill preface this with what I want to achieve. I am using the controlP5 library to create an array of List Strings(since thats the parameter needed for the DropdownList function .addItems() ), then access the specific element when the dropDownList has selected a new place in the list. so, if i select the arrow on the dropdown menu, scroll down to 'choice D', select it, I can print out the specific string within the Listof Strings. To reiterate a 3rd time, When the menu item in index is active, I want to print the string contents.

code thus far (condensed):

import controlP5.*;
import java.util.*;
ControlP5 cp5;
DropdownList d1;

int cnt = 0;

boolean check =true;
PFont f;

// Variable to store text currently being typed
String typing = "";

// Variable to store saved text when return is hit
String saved = "";
boolean startText = false;
String textValue = "";

List<String> carriers  = Arrays.asList("@txt.att.net", "@vtext.com", "@tmomail.net", "@message.alltel.com", 
"@myboostmobile.com", "@csouth1.com", "@sms.mycricket.com", "@mymetropcs.com", "@email.uscc.net", "@vmobl.com"
);
int carrierIndex;


void setup() {
  size(500, 500);
  f = createFont("Arial", 32, true);

  cp5 = new ControlP5(this);
  // create a DropdownList
  d1 = cp5.addDropdownList("cellular providers")
    .setPosition(20, 200)
      ;

  customize(d1); // customize the first list
  d1.setIndex(1);

  cp5.addTextfield("phone number")
    .setPosition(5, 100)
      .setSize(240, 40)
        .setFont(f)
          .setFocus(true)
            .setColor(color(255))
              .setColorBackground(color(60))
                ;
}

void draw() {
  background(190);

  int indent = 25;

  // Set the font and fill for text
  textFont(f);
  fill(0);


  //text(cp5.get(Textfield.class,"input").getText(), 360,130);
  // text(textValue, 360,180);

  textValue = saved;
  // Display everything
  text("Type the phone number &  select cellular provider to spam. ", 19, 40);
  //text(typing,360,180);
  text(saved, 360, 180);
}



void keyPressed() {


  // If the return key is pressed, save the String and clear it
  if (key == '\n' ) {
    // A String can be cleared by setting it equal to ""

    saved = typing;
    startText = true;
    //typing = "";
  } else {
    startText = false;
    // Otherwise, concatenate the String
    // Each character typed by the user is added to the end of the String variable.
    typing = typing + key;
  }
}

void customize(DropdownList ddl) {
  // a convenience function to customize a DropdownList
  ddl.setBackgroundColor(color(190));
  ddl.setItemHeight(30);
  ddl.setBarHeight(25);
  ddl.captionLabel().set("cellular providers");
  ddl.captionLabel().style().marginTop = 5;
  ddl.captionLabel().style().marginLeft = 2;
  ddl.valueLabel().style().marginTop = 8;

  for (  carrierIndex=0; carrierIndex<9; carrierIndex++) {
    ddl.addItems(carriers, 0);
  }


  ddl.scroll(0);
  ddl.setColorBackground(color(60));
  ddl.setColorActive(color(150));
}



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 ControlGroup
    println("event from group : "+theEvent.getGroup().getValue()+" from "+theEvent.getGroup());
  } 



  if (theEvent.getGroup().getValue() == 9.0) {
    println("option 2");
  } else if (theEvent.isController()) {
    println("event from controller : "+theEvent.getController().getLabel()+" from "+theEvent.getController());
  }
}
Tagged:

Answers

  • Do you mean...

      if (theEvent.isGroup()) {
        int index = (int) theEvent.getGroup().getValue();
        String name = carriers.get(index);
        println(name);
      }
    

    ...?

Sign In or Register to comment.