Cp5 library enables and disables textfields

I am trying to make a textfield with Cp5. I want the user to be able to edit the textfield, only when a button is pressed. If the button is unpressed the user cannot edit the textfield.
Is there a method in Textfield that can help me with this function?

by the way I am new to the Cp5 library . I couldn’t find any documentations about methods in the library apart from some examples. I just need buttons and text boxes for my Project. Is there a GUI library that is well documented?

Use

ControlP5.printPublicMethodsFor(Textfield.class);

to see possible methods. There is a setLock() that might work; you’ll have to try it.

JavaDocs are here: https://sojamo.de/libraries/controlP5/reference/index.html

1 Like

I tried setLock() but it didn’t work. I want the user not be able to type anything, when the textfield disabled.
I can’t find a full description for methods in JavaDocs.
for example setLock has only this information
image

Seems to work ok on my system:

import controlP5.*;

ControlP5 cp5;

String textValue = "";

public void lock() {
  cp5.get(Textfield.class, "textField").setLock(true);
}

public void unlock() {
  cp5.get(Textfield.class, "textField").setLock(false);
}

void setup() {
  size(700, 400);
  background(0);
  fill(255);
  PFont font = createFont("arial", 20);
  cp5 = new ControlP5(this);

  cp5.addTextfield("textField")
    .setPosition(60, 100)
    .setSize(150, 30)
    .setFont(createFont("arial", 18))
    .setAutoClear(false)
    .setLock(false);
  ;

  cp5.addBang("lock")
    .setPosition(240, 100)
    .setSize(60, 30)
    .getCaptionLabel().align(ControlP5.CENTER, ControlP5.CENTER)
    ;

  cp5.addBang("unlock")
    .setPosition(330, 100)
    .setSize(60, 30)
    .getCaptionLabel().align(ControlP5.CENTER, ControlP5.CENTER)
    ;
  textFont(font);
}

void draw() {
  
}
1 Like

I tried setLock() but it didn’t work.

What did you try that didn’t work?

Thank you @svan .
I still don’t know how the library works. I think I implemented the method incorrectly. I put it in the draw function. predicting that the text stayed 2 seconds locked and 2 seconds unlocked. But that didn’t work as I expected.

import controlP5.*;
ControlP5 cp5;
void setup(){
  size(400,400);
  cp5= new ControlP5(this);
  cp5.addTextfield("input")
     .setSize(100,50)
     .setPosition(100,100);
}

void draw(){
  cp5.get(Textfield.class,"input").setLock(true);
  delay(2000);
  cp5.get(Textfield.class,"input").setLock(false);
  delay(2000);
}

Two seconds is not very long and draw() is a loop which runs at a default rate of 60 frames/sec if I recall correctly, so that technique won’t work. Now you know. For your future posts it’s always a good idea to post the code that fails initially; this makes it easier for others to help you. You also posted this question in another public forum and I took the liberty to post the above solution in that forum so that others can also benefit from the answer. Thanks for your participation and willingness to provide feedback.