# G4P: GTextField text modification

**URL:** https://discourse.processing.org/t/g4p-gtextfield-text-modification/12292
**Category:** Libraries
**Created:** [June 26, 2019, 3:06am UTC](https://discourse.processing.org/t/g4p-gtextfield-text-modification/12292 "2019-06-26T03:06:03Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![graysonh](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@graysonh](https://discourse.processing.org/u/graysonh)
#### Post date: [June 26, 2019, 3:06am UTC](https://discourse.processing.org/t/g4p-gtextfield-text-modification/12292/1 "2019-06-26T03:06:03Z")

</div>

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.

```auto
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](https://forum.processing.org/two/discussion/25284/g4p-nullpointerexception-on-event-handler) [forums](https://discourse.processing.org/t/g4p-changing-text-of-a-textfield-in-its-eventhandler-method/1170) that this is due to the fact that a GTextField can’t be modified while it has focus.

So I tried this:

```auto
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?

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [June 26, 2019, 3:20am UTC](https://discourse.processing.org/t/g4p-gtextfield-text-modification/12292/2 "2019-06-26T03:20:35Z")

</div>

this is cut down from the examples:

File / Examples /  
/Contributed Libraries / G4P / G4P\_EditTextControls

```auto
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);
}

```

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [June 26, 2019, 8:23am UTC](https://discourse.processing.org/t/g4p-gtextfield-text-modification/12292/3 "2019-06-26T08:23:57Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [June 26, 2019, 9:02am UTC](https://discourse.processing.org/t/g4p-gtextfield-text-modification/12292/4 "2019-06-26T09:02:18Z")

</div>

i tried `forceUpdate()` get ERROR,  
but

```auto
  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

---

<div class="post-metadata">

### Author: ![graysonh](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@graysonh](https://discourse.processing.org/u/graysonh)
#### Post date: [June 26, 2019, 12:42pm UTC](https://discourse.processing.org/t/g4p-gtextfield-text-modification/12292/5 "2019-06-26T12:42:41Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [June 26, 2019, 3:06pm UTC](https://discourse.processing.org/t/g4p-gtextfield-text-modification/12292/6 "2019-06-26T15:06:46Z")

</div>

Yes I meant `forceBufferUpdate()` thanks @kll

---

<div class="post-metadata">

### Author: ![graysonh](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@graysonh](https://discourse.processing.org/u/graysonh)
#### Post date: [June 26, 2019, 11:10pm UTC](https://discourse.processing.org/t/g4p-gtextfield-text-modification/12292/7 "2019-06-26T23:10:58Z")

</div>

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.

```auto
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();
    }
  }

} 

```

---

<div class="post-metadata">

### Author: ![graysonh](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@graysonh](https://discourse.processing.org/u/graysonh)
#### Post date: [June 26, 2019, 11:22pm UTC](https://discourse.processing.org/t/g4p-gtextfield-text-modification/12292/8 "2019-06-26T23:22:40Z")

</div>

In this [thread](https://discourse.processing.org/t/cant-handle-the-loop/3375/8) 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.

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [June 27, 2019, 11:42am UTC](https://discourse.processing.org/t/g4p-gtextfield-text-modification/12292/9 "2019-06-27T11:42:40Z")

</div>

> [@graysonh](#):
>
> If there is a solution that would allow setText to work while the GTextField has focus

The short answer is there is no solution.
