G4P textArea Questions (delete text in textArea, textArea text jittering)

Hi There,
first of all: the G4P GUI builder is awesome, thanks for such a great tool.
I am using the tool together with the grafica library to make a graphic plotter applet with multiple windows (main, settings, help…). I’ve come a long way and managed to get almost anything to work, googling questions i had along the way. But now i have to ask some questions:

  1. textArea: delete the Text. I found this similar topic https://forum.processing.org/one/topic/g4p-how-to-clear-delete-text-from-a-gtextfield.html but since it is about the textField, not textArea, the solution didnt work. Since you can only send strings the same size as the existing one, and replacing the text with an empty string[] also doesn’t work, i’m asking for your help. The closest i came to a solution was to replace everything with " " (spaces), but that’s not what i wanted. In the end i just want to save the entered text to a string and afterwards delete the text I entered in the textArea. In my despair I even tried to simulate a DELETE/BACKSPACE-key action targeted at the textArea (and failed).

  2. The text shown in small textAreas jitters like crazy when the program is running. If i make the areas large enough, it stops, but the “small” areas are still large enough to fit text in it (so it looks at least). Is there a lower limit on how small a textarea is allowed to be? Or known issues when it gets too small?
    EDIT: I found the lower limit to be height=60 when the font is Arial, plane, size 20, the width seems not to be critical (worked at 30). Lower than height 60 and it jitters. Guess i’ll just adjust my window to that limits.

Since it is plenty of (mostly unrelated) code in two tabs, I don’t know what specific code to post? The gui handlers are mostly filled with one-liners, otherwise untouched. I guess these are more of base-knowledge questions?

Thanks for any helpful answers.

Glad you find GUI Builder and G4P awesome, always nice to get positive feedback :grin:

So taking each point in turn lets assume we have a GTextArea called txa

textArea: delete the Text
I assume by this you want to delete all the text in the textarea in which case you can use
txa.setText("");

You can even use an array of empty strings like this
txa.setText(new String[] { "", "" });

but you cannot use an empty array (i.e. a zero length array), this doesn’t work which makes sense to me since you are not providing any Strings to work with.
txa.setText(new String[0]);

The text shown in small textAreas jitters
This is a new one on me and you are the first person to have reported it.

I must ask the question “Why are you using such a small textarea?” since without scrollbars most of the text will be un-viewable anyway, why not use a textfield instead?

Having said that I will investigate the jittering problem further.

Just in case you have further questions I have included here a small test sketch I created to try out some of your issues. You can always modify the main code to demonstrate any issues I have missed.

import g4p_controls.*;

GTextField txf;
GTextArea txa;
GLabel lbl;
GButton btn0;

void setup() {
  size(400, 500);
  txf = new GTextField(this, 10, 10, 100, 20);
  txa = new GTextArea(this, 10, 60, 100, 30);
  lbl = new GLabel(this, 10, 120, width - 20, height - 130);
  lbl.setOpaque(true);
  txf.setText(text2);
  txa.setText(text2);
  btn0 = new GButton(this, width - 120, 20, 55, 40, "Clear Text");
  btn0.addEventHandler(this, "clearText");
  btn0 = new GButton(this, width - 60, 20, 55, 40, "Copy Text");
  btn0.addEventHandler(this, "copyText");
}

void draw() {
  background(200, 200, 255);
}

public void clearText(GButton button, GEvent event) { 
  txa.setText("");
  //txa.setText(new String[0]);  // Zero element array doesn't work
  //txa.setText(new String[] { "", "" });  // Array with empty strings as elements work
}

public void copyText(GButton button, GEvent event) { 
  String s = txa.getText();
  txa.setText("");
  lbl.setText(s);
}

String text0 = "Quark exists";
String text1 = "The rain in Spain falls mainly in the plane";
String text2 = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. " 
  + "Lorem Ipsum has been the industry's standard dummy text ever since the " 
  + "1500s, when an unknown printer took a galley of type and scrambled it to" 
  + " make a type specimen book. It has survived not only five centuries, but " 
  + "also the leap into electronic typesetting, remaining essentially unchanged. " 
  + "It was popularised in the 1960s with the release of Letraset sheets " 
  + "containing Lorem Ipsum passages, and more recently with desktop publishing " 
  + "software like Aldus PageMaker including versions of Lorem Ipsum." 
  + "Contrary to popular belief, Lorem Ipsum is not simply random text. It has " 
  + "roots in a piece of classical Latin literature from 45 BC, making it over " 
  + "2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney " 
  + "College in Virginia, looked up one of the more obscure Latin words, consectetur, " 
  + "from a Lorem Ipsum passage, and going through the cites of the word in classical " 
  + "literature, discovered the undoubtable source. Lorem Ipsum comes from sections " 
  + "1.10.32 and 1.10.33 of 'de Finibus Bonorum et Malorum' (The Extremes of Good " 
  + "and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory " 
  + "of ethics, very popular during the Renaissance." 
  + "It is a long established fact that a reader will be distracted by the readable " 
  + "content of a page when looking at its layout. The point of using Lorem Ipsum " 
  + "is that it has a more-or-less normal distribution of letters, as opposed to " 
  + "using 'Content here, content here', making it look like readable English. Many " 
  + "desktop publishing packages and web page editors now use Lorem Ipsum as their " 
  + "default model text, and a search for 'lorem ipsum' will uncover many web sites " 
  + "still in their infancy. Various versions have evolved over the years, sometimes " 
  + "by accident, sometimes on purpose (injected humour and the like)";
1 Like

Thank you for the quick response!

  1. While investigating the error recreation code of yours (thanks for the expense) I think i found my error and realized i just should have posted my code from the beginning, sorry.
public void textarea3_change1(GTextArea source, GEvent event) { //_CODE_:textarea3_Eingabe:602026:
  if (event==GEvent.ENTERED){
    doEingabe(textarea3_Eingabe.getText(0)); //my function in the main tab
    textarea3_Eingabe.setText("");
    }

It has to do with the handler reacting on ENTERED. Because if i change it to something else, the code works. With “entered” i get this error instead: java.lang.StringIndexOutOfBoundsException: String index out of range: 3, where 3 is my real entered text length-1. When i change the event to LOST_FOCUS, it pretty much does what i wanted from the beginng. But whats the deal with ENTERED then?

  1. Haha, I expected you to ask me why i use such small textAreas. With your provided sample code i figured out i did not read the built in G4P reference well enough. With textArea, getText() is directly in “Public Member Functions”, with TextField it is under " Methods inherited from class g4p_controls.GEditableTextControl" an i simply overlooked this and thought from that moment on i had to use textArea if I want to get more than just a number value out of it.

So you pretty much solved all my issues in one go, big thanks for that. If you just could tell me why the ENTERED event throws an error, that would be great.

OK I have checked the source code and there is no bug. The jittering is caused by the code scrolling the text inside the textarea to keep the text insertion point in the visible area. If the GTextArea is too short then it wants to both scroll up and scroll down and flips between the two causing the jitter.

I worked on the assumption that anyone using a GTextArea will want to see at least 2 lines of text otherwise they would have used a GTextField. This seems a reasonable assumption.

So the minimum height of a GTextArea is
= 2 * line height + 12 pixels
now the line height includes the spacing between lines so is ~1.5 * font height so that becomes
~ 3 * font height + 12 pixels

Yes i guess this is absolutely fine, but you know the end user (me) is always stupid enough to do unreasonable things and use things the way they were not designed for. Since I now know all the answers, i’m going to use the controls as intended :grin:

I have no idea since I couldn’t duplicate the problem but if you have a string on length 3 then the indices go between 0-2 so using an index value of 3 would be out of range.

It is always best to check the type of event GTextField and GTextArea controls can fire
ENTERED
CHANGED
SELECTION_CHANGED
GETS_FOCUS
LOST_FOCUS
so pick the one that does what you want.

I recreated it with your sample sketch, so you can see what happens if you want.
If you type a text in the textArea and hit enter, the error occurs.

import g4p_controls.*;

GTextField txf;
GTextArea txa;
GLabel lbl;
GButton btn0;

void setup() {
  size(400, 500);
  txf = new GTextField(this, 10, 10, 100, 20);
  txa = new GTextArea(this, 10, 60, 100, 30);
  lbl = new GLabel(this, 10, 120, width - 20, height - 130);
  lbl.setOpaque(true);
  txf.setText(text2);
  txa.setText(text2);
  txa.addEventHandler(this, "enterText");
  btn0 = new GButton(this, width - 120, 20, 55, 40, "Clear Text");
  btn0.addEventHandler(this, "clearText");
  btn0 = new GButton(this, width - 60, 20, 55, 40, "Copy Text");
  btn0.addEventHandler(this, "copyText");
}

void draw() {
  background(200, 200, 255);
}

public void clearText(GButton button, GEvent event) { 
  txa.setText("");
  //txa.setText(new String[0]);  // Zero element array doesn't work
  //txa.setText(new String[] { "", "" });  // Array with empty strings as elements work
}

public void copyText(GButton button, GEvent event) { 
  String s = txa.getText();
  txa.setText("");
  lbl.setText(s);
}

public void enterText(GTextArea txa, GEvent event) { 
  if (event==GEvent.ENTERED){
      String s = txa.getText();
      txa.setText("");
      lbl.setText(s);
  }
}
String text0 = "Quark exists";
String text1 = "The rain in Spain falls mainly in the plane";
String text2 = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. " 
  + "Lorem Ipsum has been the industry's standard dummy text ever since the " 
  + "1500s, when an unknown printer took a galley of type and scrambled it to" 
  + " make a type specimen book. It has survived not only five centuries, but " 
  + "also the leap into electronic typesetting, remaining essentially unchanged. " 
  + "It was popularised in the 1960s with the release of Letraset sheets " 
  + "containing Lorem Ipsum passages, and more recently with desktop publishing " 
  + "software like Aldus PageMaker including versions of Lorem Ipsum." 
  + "Contrary to popular belief, Lorem Ipsum is not simply random text. It has " 
  + "roots in a piece of classical Latin literature from 45 BC, making it over " 
  + "2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney " 
  + "College in Virginia, looked up one of the more obscure Latin words, consectetur, " 
  + "from a Lorem Ipsum passage, and going through the cites of the word in classical " 
  + "literature, discovered the undoubtable source. Lorem Ipsum comes from sections " 
  + "1.10.32 and 1.10.33 of 'de Finibus Bonorum et Malorum' (The Extremes of Good " 
  + "and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory " 
  + "of ethics, very popular during the Renaissance." 
  + "It is a long established fact that a reader will be distracted by the readable " 
  + "content of a page when looking at its layout. The point of using Lorem Ipsum " 
  + "is that it has a more-or-less normal distribution of letters, as opposed to " 
  + "using 'Content here, content here', making it look like readable English. Many " 
  + "desktop publishing packages and web page editors now use Lorem Ipsum as their " 
  + "default model text, and a search for 'lorem ipsum' will uncover many web sites " 
  + "still in their infancy. Various versions have evolved over the years, sometimes " 
  + "by accident, sometimes on purpose (injected humour and the like)";

Anyways, thanks for your help!

This is the event handler for the textarea control txa you are not allowed to modify the text of a control in its own event handler so the statement
txa.setText("");
is not permitted inside the event handler or in any method called by the event handler.

1 Like

Thanks for the clarification. I thought something like that, changing a text in a handler that reacts to changing text didnt sound quite right.