3

I would like to remove some of the functionality of a Textfield.

When the enter or return keys are pressed, the field becomes empty. I would like the value entered to stay within the field.

I have tried overriding the submit method but this hasn't done the job:

widthTopField = new Textfield(controlP5, "widthField"){
    @ Override public Textfield submit(){
        return this;
    }
};
JMP
  • 4,417
  • 17
  • 30
  • 41
MLast
  • 60
  • 7

1 Answers1

2

Demonstrates using .setAutoClear() to retain edit field contents after hitting return button. Field 1 is set to true and the other is set to false.

import controlP5.*;

ControlP5 cp5;

void setup() {
  size(400,400);
  
  PFont font = createFont("arial",18);
  
  cp5 = new ControlP5(this);
  
  cp5.addTextfield("Field 1")
     .setPosition(40,50)
     .setSize(200,40)
     .setFont(font)
     .setFocus(true)
     .setColor(color(255))
     .setAutoClear(true);
     ;
                 
  cp5.addTextfield("Field 2")
     .setPosition(40,130)
     .setSize(200,40)
     .setFont(font)
     .setAutoClear(false);
     ;
}

void draw() {
  background(0);
}

apodidae
  • 1,988
  • 2
  • 5
  • 9
  • Good work! Is there a reason why there is 2 fields to demonstrate `.setAutoClear(false);` ? – laancelot Oct 27 '20 at 22:02
  • Redundant third field removed. – apodidae Oct 27 '20 at 23:59
  • That wasn't a critic but thanks nonetheless, it's an improvement. I was just wondering what I was missing. – laancelot Oct 28 '20 at 02:03
  • 1
    @laancelot There was nothing to miss and there was a reason why it wound up that way, but I don't want to waste bandwidth trying to explain. I agree that a good demo has no unnecessary lines of code. Thanks for the suggestion. – apodidae Oct 28 '20 at 03:33