G4P: GTextField text modification

Hi all,

I am using a GTextField to allow the user to specify values being used in other processes. In the event handler for the GTextField I want to check what’s being typed and make sure that it is a valid value for my program. Below is what I have tried to accomplish this.

public void matOption8TextChanged(GTextField source, GEvent event) { 
  println("matOption8Text - GTextField >> GEvent." + event + " @ " + millis());

  String temp = source.getText();
  String[] match = match(temp, "^([0-5][.]([0-9]{1,3})?)$|^([0-5])$");
  if (match == null) {
    if (temp.length() > 0) {
      source.setText(temp.substring(0, temp.length()));
    }
  }

} 

When I attempt to modify the text I get a NullPointerException. I have read on other forums that this is due to the fact that a GTextField can’t be modified while it has focus.

So I tried this:

public void matOption8TextChanged(GTextField source, GEvent event) { 
  println("matOption8Text - GTextField >> GEvent." + event + " @ " + millis());

  String temp = source.getText();
  String[] match = match(temp, "^([0-5][.]([0-9]{1,3})?)$|^([0-5])$");
  if (match == null) {
    if (temp.length() > 0) {
      source.setFocus(false);
      source.setText(temp.substring(0, temp.length()));
      source.setFocus(true);
    }
  }

} 

This does stop processing from throwing a NullPointerException, but the text in the text field is never changed.

Does anyone have an idea of a workaround that I could try to implement?

Also, I thought that maybe instead of modifying the user’s input, I could just change the color of the text red to signal that it is an unaccepted value. Is there a way to change to the color of the text in a GTextField programmatically?

1 Like

this is cut down from the examples:

File / Examples /
/Contributed Libraries / G4P / G4P_EditTextControls

import g4p_controls.*;

GTextField txf1;

public void setup() {
  size(300, 300);
  txf1 = new GTextField(this, 10, 10, 200, 20);
  txf1.setFocus(true);

}

public void draw() {
  background(200, 200,0);
}

public void displayEvent(String name, GEvent event) {
  String extra = " event fired at " + millis() / 1000.0 + "s";
  print(name + "   ");
  switch(event) {
  case CHANGED:
    println("CHANGED " + extra);
    break;
  case SELECTION_CHANGED:
    println("SELECTION_CHANGED " + extra);
    break;
  case LOST_FOCUS:
    println("LOST_FOCUS " + extra);
    break;
  case GETS_FOCUS:
    println("GETS_FOCUS " + extra);
    break;
  case ENTERED:
    println("ENTERED " + extra);  
    break;
  default:
    println("UNKNOWN " + extra);
  }
}

public void handleTextEvents(GEditableTextControl textControl, GEvent event) { 
  displayEvent(textControl.tag, event);
}

After source.setFocus(true); try adding the statement source.forceUpdate();

If that doesn’t work post here and I will try again when I am on my computer.

2 Likes

i tried forceUpdate() get ERROR,
but

  txf1 = new GTextField(this, 10, 10, 200, 20);
  txf1.setFocus(true);
  txf1.forceBufferUpdate();  

but in both cases best is a
mouse click on the text field

1 Like

Thanks for the advice! I’ll try both of those when I am at home again. I just realized while looking over my code that I am not actually modifying the users string at all in my use of substring. I am just returning the entire original string. That could be (probably is) why I wasn’t seeing any change after modifying the text field.

Hopefully the fix is as simple as correcting that line of code. Either way I’ll reply here to give an update.

Yes I meant forceBufferUpdate() thanks @kll

1 Like

Ok, I tried using forceBufferUpdate() as shown below. The issue is that now when the setFocus(false) method is called it jumps to the beginning of the event handler method and creates an infinite loop.

public void matOption8TextChanged(GTextField source, GEvent event) { 
  println("matOption8Text - GTextField >> GEvent." + event + " @ " + millis());

  String temp = source.getText();
  String[] match = match(temp, "^([1-5][.]([0-9]{1,3})?)$|^([1-5])$");
  if (match == null) {
    if (temp.length() > 0) {
      source.setFocus(false);
      source.setText(temp.substring(0, temp.length()-1));
      source.setFocus(true);
      source.forceBufferUpdate();
    }
  }

} 
1 Like

In this thread a similar issue is discussed. I modified my code so the color changes based on if it meets the regex or not. If there is a solution that would allow setText to work while the GTextField has focus I would really like to know about it.

The short answer is there is no solution.

1 Like