Changing Text of Label in ControlP5

I am using the ControlP5 GUI library.
I want to change the text displayed in TextLabel when triggered by some other event. For example in the following case when I press the button.

So I tried two methods marked by OPTION 1 and OPTION 2 in the following code.
Option 2 is working but Option 1 is not working.
I don’t want to declare a label variable. so how can I make Option 1 work?
Please Help.

import controlP5.*;

ControlP5 cp5;

Textlabel myTextlabelA;

void setup() {
  size(700,400);
  cp5 = new ControlP5(this);
  
  myTextlabelA = cp5.addTextlabel("label")
                    .setText("A single ControlP5 textlabel, in yellow.")
                    .setPosition(100,50)
                    .setColorValue(0xffffff00)
                    .setFont(createFont("Georgia",20))
                    ;
  
  cp5.addToggle("AAA").setPosition(200,200).align(CENTER,CENTER,CENTER,CENTER);

}



void draw() {
  background(0);
}

void AAA(int x){
  cp5.getController("label").setText("Value="+x);  /// OPTION 1
  //myTextlabelA.setText("Value="+x);              /// OPTION 2
}
void AAA(int x){
  cp5.getController("label").setValueLabel("Value="+x);  /// OPTION 1
}
2 Likes

Thank you very much.

How to similarly update the text in textarea controller?
Following code is giving exception.


import controlP5.*;

ControlP5 cp5;


void setup() {
  size(700,400);
  cp5 = new ControlP5(this);
  
  cp5.addTextarea("label")
                    .setText("A single ControlP5 textlabel, in yellow.")
                    .setPosition(100,50)
                    .setColorValue(0xffffff00)
                    .setFont(createFont("Georgia",20)).setColorBackground(200)
                    ;
  
  cp5.addToggle("AAA").setPosition(200,200).align(CENTER,CENTER,CENTER,CENTER);

}



void draw() {
  background(200);
}

void AAA(int x){
  cp5.getController("label").setValueLabel("Val: "+x);
}

I don’t think using the controller for textArea will work because the following returns null.

println(cp5.getController("label")); // returns null

label.setText("Val: "+x); works ok.