Link slider to textfield with controlp5

edited November 2013 in Library Questions

I am fairly new to processing and the controlp5 library. I am wanting to link a textfield and a slider together so that if the slider is moved the slider value displays in the textfield. I also want it to work the other way around where if a number is typed in the textfield it will update the value in the slider. All the examples I can find show just the first option. Is there a way to get both to display the same values as I described? Thank in advance for any help you can provide.


  • I have the Slider controlling the Textfield but I still can not seem to get the textfield to adjust the Slider. This is the code I currently have:

    //imports controlp5 Controls
    import controlP5.*;
    ControlP5 cp5;
    ControlFont font;
    String speedValue = "";
    Textfield speedInput;
    String sliderValue = "";
    Slider mySlider;
    void setup(){
     // ControlP5 Inputs
     cp5 = new ControlP5(this);
     //Changes font for all ControlP5 Items
      PFont pfont = createFont("Arial",16,true);
      font = new ControlFont(pfont);
      // change the original colors of ControlP5 items
       // adds Text Field
      speedInput = cp5.addTextfield("Speed",5,20,50,20);
      //adds slider
      //("name",theMin, theMax, theDefault, theX, theY, theW, theH)
    void draw(){
     //Draw initial sketch
    public void Speedx10ms(int theValue) {
      Textfield txt = ((Textfield)cp5.getController("Speed"));
    public void Speed(String theValue) {
     Slider sld = ((Slider)cp5.getController("Speedx10ms"));
      println("### got an event from Speed : "+theValue);

    I am getting an error message that says "The method setValue(float) in the type Slider is not applicable for the arguments (String)" Any ideas on how to get this to work?

  • edited November 2013

    Well, if it says it expects a float data-type, you gotta provide exactly that! #-o
    1st, you gotta declare variable theValue as float instead of String @ #56.
    Then @ #58, remove "" +, lest the expression results in a String again, due to concatenation w/ theValue! 8-X

  • Problem solved. I revised the last block of the coded to this:

    public void Speed(String theValue) {
      println("### got an event from Speed : "+theValue);

    now it works fine.

  • edited November 2013 Answer ✓

    Congratz! Now I've realized that addTextfield() from ControlP5 demands that Speed() have a String parameter! :@)
    But your problem is that setValue() wants a float, and you gotta convert that from a String!

    I'm not used w/ most 3rd party libraries yet! :-\"

Sign In or Register to comment.