# Cp5 library enables and disables textfields

**URL:** <https://discourse.processing.org/t/cp5-library-enables-and-disables-textfields/38979>\
**Category:** Libraries\
**Created:** [September 26, 2022, 1:11pm UTC](https://discourse.processing.org/t/cp5-library-enables-and-disables-textfields/38979 "2022-09-26T13:11:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![maro](https://avatars.discourse-cdn.com/v4/letter/m/96bed5/32.png) [@maro](https://discourse.processing.org/u/maro)\
**Post date:** [September 26, 2022, 1:11pm UTC](https://discourse.processing.org/t/cp5-library-enables-and-disables-textfields/38979/1 "2022-09-26T13:11:04Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [September 26, 2022, 3:55pm UTC](https://discourse.processing.org/t/cp5-library-enables-and-disables-textfields/38979/2 "2022-09-26T15:55:30Z")

</div>

Use

```auto
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](https://sojamo.de/libraries/controlP5/reference/index.html)

---

<div class="post-metadata">

**Author:** ![maro](https://avatars.discourse-cdn.com/v4/letter/m/96bed5/32.png) [@maro](https://discourse.processing.org/u/maro)\
**Post date:** [September 26, 2022, 10:11pm UTC](https://discourse.processing.org/t/cp5-library-enables-and-disables-textfields/38979/3 "2022-09-26T22:11:31Z")

</div>

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](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/0/d/0d6dd8757a23a15178be61634506370c7debe421.png)

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [September 26, 2022, 11:59pm UTC](https://discourse.processing.org/t/cp5-library-enables-and-disables-textfields/38979/4 "2022-09-26T23:59:36Z")

</div>

Seems to work ok on my system:

```auto
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() {
  
}

```

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [September 27, 2022, 1:38am UTC](https://discourse.processing.org/t/cp5-library-enables-and-disables-textfields/38979/5 "2022-09-27T01:38:37Z")

</div>

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

What did you try that didn’t work?

---

<div class="post-metadata">

**Author:** ![maro](https://avatars.discourse-cdn.com/v4/letter/m/96bed5/32.png) [@maro](https://discourse.processing.org/u/maro)\
**Post date:** [September 27, 2022, 12:39pm UTC](https://discourse.processing.org/t/cp5-library-enables-and-disables-textfields/38979/6 "2022-09-27T12:39:17Z")

</div>

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.

```auto
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);
}

```

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [September 27, 2022, 3:45pm UTC](https://discourse.processing.org/t/cp5-library-enables-and-disables-textfields/38979/7 "2022-09-27T15:45:30Z")

</div>

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.
