Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

Controlp5 Basic Example - Text Input Field

$
0
0

I'm new to ControlP5 (and Processing), and want to allow the user to enter two strings (URLs) into a text field, which will then pass into a variable once a Submit button is pressed.

In my example below, I'd like to pass the "textInput_1" and "textInput_2"and pass it into a stored variable that I can user else where.

Thanks!

Code:

import controlP5.*;

ControlP5 cp5;

String textValue = "";

void setup() {
  size(700, 400);

  PFont font = createFont("arial", 20);

  cp5 = new ControlP5(this);

  cp5.addTextfield("textInput_1")
    .setPosition(20, 100)
      .setSize(200, 40)
        .setFont(font)
          .setFocus(true)
            .setColor(color(255, 0, 0))
              ;

  cp5.addTextfield("textInput_2")
    .setPosition(20, 170)
      .setSize(200, 40)
        .setFont(createFont("arial", 20))
          .setAutoClear(false)
            ;

  cp5.addBang("Submit")
    .setPosition(240, 170)
      .setSize(80, 40)
        .getCaptionLabel().align(ControlP5.CENTER, ControlP5.CENTER)
          ;

  textFont(font);
  intro();
}



void intro () {

  background(0);
  fill(255);
  text(cp5.get(Textfield.class, "textInput_1").getText(), 360, 130);
  text(textValue, 360, 180);
}


void controlEvent(ControlEvent theEvent) {
  if (theEvent.isAssignableFrom(Textfield.class)) {
    println("controlEvent: accessing a string from controller '"
      +theEvent.getName()+"': "
      +theEvent.getStringValue()
      );
  }

}


public void input(String theText) {
  // automatically receives results from controller input
  println("a textfield event for controller 'input' : "+theText);
}

void draw () {

}

void showInput () {

  //println(textInput_1) ;
  // println(textInput_2) ;
}

Viewing all articles
Browse latest Browse all 2896

Trending Articles