Update a text field with a slider?

edited November 2013 in Library Questions

I have a text field which displays a number and 8 sliders above which affect the value of that number. The text field updates to the correct number according to the slider values when clicked, however I was wondering if it was possible to automatically update the text field when a slider is moved? This would negate the need to click on the text field each time. This is the code for one of the sliders (they are all basically the same) and the text field.

public void slider1_change1(GSlider source, GEvent event) { //_CODE_:slider1:402621:
  int value = source.getValueI();
  int[] next = ca.getRules();
  next[7] = value;
  ca.setRules(next);
} //_CODE_:slider1:402621:

public void textfield1_change1(GTextField source, GEvent event) { //_CODE_:textfield1:757255:
  int toConvert = ca.returnRuleNumber(ca.getRules());
  String ruleNumber = str(toConvert);
  String text = ("This is Rule " + ruleNumber);
  source.setText(text);
} //_CODE_:textfield1:757255:

Thanks.

Answers

  • Wouldn't it be easier to use a label instead of a textfield? I use a ControlP5 slider in my application to update a ControlP5 label and it's pretty straight-forward.

    The slider event triggers a procedure that sets a global string variable. In my void draw() that variable value is continuously assigned to the label, so anytime the variable changes (when the slider is activated) it's displayed on the label.

  • Thank you very much, solved it for me, I appreciate it.

  • Whats wrong with

    public void slider1_change1(GSlider source, GEvent event) { //_CODE_:slider1:402621:
      int value = source.getValueI();
      int[] next = ca.getRules();
      next[7] = value;
      ca.setRules(next);
      textfield1.setText("This is Rule " + value);
    } //_CODE_:slider1:402621:
    

    It changes the value displayed in textField1 as the slider is dragged.

    If you are using textField1 simply to display values and if you don't want to change the text via the keyboard then I suggest you use a GLabel instead (you can always set 'Opaque' to true to always make it visible).

Sign In or Register to comment.