G4P giving me an error on TextField settext()

I did a form with G4P GUI. I added only one line at the event of a text field.
It looks that the event can’t handle the typing of one character at the field and crashes.

I am getting this error while executing the code below.

   G4P V4.3.6 created by Peter Lager
textfield1 - GTextField >> GEvent.GETS_FOCUS @ 9017
textfield1 - GTextField >> GEvent.CHANGED @ 10347
	at g4p_controls.GTextField.keyEvent(Unknown Source)
	at g4p_controls.GWindowImpl.sendKeyEvent(Unknown Source)
	at g4p_controls.GWindowImpl.keyEvent(Unknown Source)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at processing.core.PApplet$RegisteredMethods.handle(PApplet.java:1436)
	at processing.core.PApplet.handleMethods(PApplet.java:1638)
	at processing.core.PApplet.handleKeyEvent(PApplet.java:3021)
	at processing.core.PApplet.dequeueEvents(PApplet.java:2655)
	at processing.core.PApplet.handleDraw(PApplet.java:2493)
	at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1547)
	at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:313)
Could not run the sketch (Target VM failed to initialize).
For more information, read revisions.txt and Help → Troubleshooting.

The line in conflict is:

textfield1.setText("any text"); // <---------- THIS LINE CONFLICTS

Here is the code and the conflicting line is at line 30

import g4p_controls.*;

void setup() {
  size(640, 360);

void draw() {


/* =========================================================
 * ====                   WARNING                        ===
 * =========================================================
 * The code in this tab has been generated from the GUI form
 * designer and care should be taken when editing this file.
 * Only add/edit code inside the event handlers i.e. only
 * use lines between the matching comment tags. e.g.

 void myBtnEvents(GButton button) { //_CODE_:button1:12356:
     // It is safe to enter your event code here  
 } //_CODE_:button1:12356:
 * Do not rename this tab!
 * =========================================================

public void textfield1_change1(GTextField source, GEvent event) { //_CODE_:textfield1:346564:
  println("textfield1 - GTextField >> GEvent." + event + " @ " + millis());
  textfield1.setText("any text"); // <---------- THIS LINE CONFLICTS
} //_CODE_:textfield1:346564:

// Create all the GUI controls. 
// autogenerated do not edit
public void createGUI(){
  surface.setTitle("Sketch Window");
  textfield1 = new GTextField(this, 20, 10, 120, 30, G4P.SCROLLBARS_NONE);
  textfield1.addEventHandler(this, "textfield1_change1");

// Variable declarations 
// autogenerated do not edit
GTextField textfield1;

Hey, I’m not sure, but I think it’s because you call setText in the Change function and so the function would call itself indefinitely even if the text remains the same. That’s the only problem I can think of. For example, it would work if you call setText () in the setup function after createGUI ().

1 Like

It will work in the setup.
It will not in the draw
It looks like it can’t be called more than once.
My need is to change some characters if I don´t let those in the text typed.
So how can I reassign another value to that field?

@Flolo has it right - :grin: but not for the correct reason, although infinite recursion might seem a possibility.

In GTextField and GTextArea controls it is not permitted to change the displayable text inside its event handler. These controls support multiple font size, styles layouts and other nifty features (try the example that comes with the library). To do this I have had to use many low level Java library classes and created a new class StyledString.

Unfortunately this means that changing the text within the event handler is a no-no. What you can do is ‘test’ the text for validity and change the font colours or something similar based on whether the text is valid or not but you are not allowed to change the text.

First of all, thanks Quark for your great work.
It would be nice to have the chance to do this dynamic text change.
Imagine that you have to check syntax or force hyphens in your input like you can do in Javascript that you can check the text with every strike of a key.
Thanks for the answer!