cp5 textArea multiline

edited February 2018 in Library Questions

Hello.

I wants to show text in the Textarea ouput in mode multiline: every time I press Return on TextField input shows a different line but don't work! any solution? please : )


import controlP5.*;

ControlP5 cp5;

String textValue = "";

Textfield input;
Textarea output;

StringList script;

int index = 0;
void setup()
{
  size(1000,700, P3D);
  frameRate(15);

  PFont font = createFont("arial",20);

  cp5 = new ControlP5(this);

  script = new StringList();

  input = cp5.addTextfield("input")
       .setColor(color(0))
       .setColorBackground(color(255,255,255,29))
       .setColorCursor(color(0))
       .setPosition(10,10)
       .setSize(980,40)
       .setFont(font)
       .setFocus(true)
       ;

  output = cp5.addTextarea("output")
       .setColor(color(0))
       .setColorBackground(color(255,255,255,29))
       //.setColorCursor(color(0))
       .setPosition(10,70)
       .setSize(980,600)
       .setFont(font)
       ;

}

void draw()
{
  background(255);

}

void controlEvent(ControlEvent theEvent){
  index = index + 1;
  textValue = input.getText();
  //println(textValue);
  script.append(textValue);
  String item = script.get(index-1);
  item +=item + "\n";
  output.setText(item + "\n");
  //println(script);
}

Code formatted by moderator 23/02/2018

Answers

  • Please, format your code when you create a post. To do that, highlight your lines of code and press ctrl+o. Ensure there is an empty line above and below the code. Here next I present a solution for you to consider:

    import controlP5.*;
    
    ControlP5 cp5;
    
    Textfield input; 
    Textarea output;
    
    String textValue = "";
    StringList script;
    int index = 0; 
    
    void setup() { 
      size(1000, 700, P3D); 
      frameRate(15);
    
      PFont font = createFont("arial", 20);
    
      cp5 = new ControlP5(this);
    
      script = new StringList();
      input = cp5.addTextfield("input") 
      .setColor(color(0)) 
      .setColorBackground(color(255, 255, 255, 29)) 
      .setColorCursor(color(0)) 
      .setPosition(10, 10) 
      .setSize(980, 40) 
      .setFont(font) 
      .setFocus(true) ;
    
      output = cp5.addTextarea("output") 
      .setColor(color(0)) 
      .setColorBackground(color(255, 255, 255, 29)) //.setColorCursor(color(0)) 
      .setPosition(10,70) 
      .setSize(980,600) 
      .setFont(font) ;
    }
    
    void draw() { 
      background(255);
    }
    
    void controlEvent(ControlEvent theEvent) { 
      index = index + 1; 
      textValue = input.getText(); 
      //println(textValue); 
      script.append(textValue+"\n"); 
      //String item = script.get(index-1); 
      //item +=item + "\n"; 
      //output.setText(item + "\n"); 
      output.clear();
    
      String fullStr="";
      for(String s:script)
        fullStr+=s;
    
      output.setText(fullStr);
    }
    

    I hope this helps,

    Kf

  • @kfrajer -- if you would, could you say something very briefly about what the mistake was, and why the solution works? Code-only solutions tend to be hard to understand for some beginners experiencing the same problem.

  • I agree with you @jeremydouglass . However it is hard to provide full detail as initial code was not formatted. From my version of the code, I did a small modification to the controlEvent() function. The problem was that data was not been appended from the input to the output textField. I remove the culprit line 47 and modified line 46 so it added a line separator. Then I added line 50 to 56 to reset/clear the output text field and then join all string objects under a single String variable, fullStr before setting it in the the textField output.

    Kf

  • Thanks kfrajer, your solutions work! Sorry for not format de code.

    For jeremydouglas: I tried a lot of solutions but the magic is in the:

       String fullStr="";
      for(String s:script)
        fullStr+=s;
    

    There is a really god solution.``

Sign In or Register to comment.