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.

Answers

  • 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(){
     size(500,75);
     smooth();
    
     // 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
      cp5.setColorForeground(0xff2400b3);
      cp5.setColorBackground(0xff000000);
      cp5.setColorLabel(0xff000000);
      cp5.setColorValue(0xffffffff);
      cp5.setColorActive(0xff3502ff);
    
       // adds Text Field
      speedInput = cp5.addTextfield("Speed",5,20,50,20);
      speedInput.setColorForeground(127);
    
      cp5.setControlFont(pfont);
    
      //adds slider
      //("name",theMin, theMax, theDefault, theX, theY, theW, theH)
       cp5.addSlider("Speedx10ms",0,255,100,65,20,300,20);
    }
    
    void draw(){
    
     //Draw initial sketch
     background(127);
     stroke(255);
    }
    public void Speedx10ms(int theValue) {
      Textfield txt = ((Textfield)cp5.getController("Speed"));
      txt.setValue(""+theValue);
    
    }
    
    
    
    public void Speed(String theValue) {
     Slider sld = ((Slider)cp5.getController("Speedx10ms"));
      sld.setValue(""+theValue);
      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) {
      cp5.controller("Speedx10ms").setValue(int(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.