controlp5 behaving differently in setup and draw

Hi all, I have a difficulty with controlp5. I am trying to update the value in a textbox but it won't accept my new string. The code below is my test code to get this working.

In the setup I can pass a string into the .setValue(); and it works but when it comes to the draw I can no longer pass a string into it, it throws an error that says it expects a float.

So the question is why? I can't for the life of me work out why this works then doesn't.

Thanks.

import controlP5.*;

ControlP5 cp5;

String newInput = "boop";

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

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

  cp5 = new ControlP5(this);

  cp5.addTextfield("Word 1")
    .setPosition(20, 170)
    .setSize(200, 40)
    .setFont(font)
    .setAutoClear(true)
    .setValue(newInput)
    .setColor(255) //text color
    .setColorBackground(0)
    .setCaptionLabel(" ")
    ;

  textFont(font);
}

void draw() {
  background(210);
  newInput = cp5.getController("Word 1").getStringValue();
  cp5.getController("Word 1").setValue(newInput);
  println("NI:" + newInput);
  text(newInput, mouseX, mouseY);
}

Answers

  • edited October 2016 Answer ✓

    P.S.: The reason why in setup() method setValue() works w/ a String argument is b/c at that moment Java sees the object as of datatype Textfield due to addTextfield() method, which creates it.
    However when returning that same object via getController(), Java thinks it is datatype Controller instead of its actual Textfield. And thus an upcasting is necessary in order to access the extra members from Textfield subclass. 8-|

  • It appears to me that setStringValue() isn't able to set the value of the text to appear in the textbox whereas setValue() can. Is this correct?

    Other than that thankyou GoToLoop once again for the timely and wonderfully useful answer.

Sign In or Register to comment.