# How to delete a G4P GTextField

**URL:** <https://discourse.processing.org/t/how-to-delete-a-g4p-gtextfield/31897>\
**Category:** Libraries\
**Tags:** homework\
**Created:** [August 24, 2021, 8:17pm UTC](https://discourse.processing.org/t/how-to-delete-a-g4p-gtextfield/31897 "2021-08-24T20:17:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jos](https://avatars.discourse-cdn.com/v4/letter/j/2bfe46/32.png) [@Jos](https://discourse.processing.org/u/Jos)\
**Post date:** [August 24, 2021, 8:17pm UTC](https://discourse.processing.org/t/how-to-delete-a-g4p-gtextfield/31897/1 "2021-08-24T20:17:28Z")

</div>

I want to use an G4P object just temporarily. For example a text field. But once created I can’t delete it any more. Is there a solution?

```auto
import g4p_controls.*;

void setup()
{ fill(200); size(400,200);
  fill(255); rect(300,0,100,100); rect(300,100,100,100);
  fill(0); text("create",320,50); text("delete",320,150);
}

void draw()
{
}

void mousePressed()
{ if(mouseX>300 && mouseX<400 && mouseY>0 && mouseY<100) // create
  { fill(100); rect(110,70,70,20); // rectangle
    
    GTextField gtf=new GTextField(this,110,130,70,20); // text field
    gtf.tag="gtf";
    gtf.setText("hello");
    gtf.setFocus(true);
  }
  if(mouseX>300 && mouseX<400 && mouseY>100 && mouseY<200) // delete
  { fill(200); rect(0,0,300,200); // rectangle
  
    // how to delete the text field or make it invisible ????
  }
}

void handleTextEvents(GEditableTextControl textControl, GEvent event) 
{ switch(event)
  { case ENTERED: if(textControl.tag=="gtf") println("entered"); break;
    default:
  }
}

```

---

<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:** [September 6, 2021, 6:38am UTC](https://discourse.processing.org/t/how-to-delete-a-g4p-gtextfield/31897/2 "2021-09-06T06:38:43Z")

</div>

@Jos Sorry I missed this discussion first time round and just received a notification from Sourceforge with the feature request.

For future reference include @quark in any discussion where you want my attention 😀

This feature already exists and the sketch code below demonstrates it in action. The alternative is just to switch the control between visible and invisible as and when required. This is useful if you want to be able to reuse the control multiple times and is very much more efficient.

```auto
import g4p_controls.*;

GTextField txf; 
GButton btnRemove; 

public void setup(){
  size(240, 120);
  createGUI();  
}

public void draw(){
  background(240);
  line(10,10,width-20,height-20);
}

public void txf_event_handler(GTextField source, GEvent event) {
  println("txf - GTextField >> GEvent." + event + " @ " + millis());
}

public void remove_click(GButton source, GEvent event) {
  println("btnRemove - GButton >> GEvent." + event + " @ " + millis());
  txf.dispose();
}

// Create all the GUI controls. 
public void createGUI(){
  G4P.messagesEnabled(false);
  G4P.setGlobalColorScheme(GCScheme.BLUE_SCHEME);
  G4P.setMouseOverEnabled(false);
  surface.setTitle("Sketch Window");
  txf = new GTextField(this, 10, 10, 120, 30, G4P.SCROLLBARS_NONE);
  txf.setOpaque(true);
  txf.addEventHandler(this, "txf_event_handler");
  btnRemove = new GButton(this, 10, 50, 120, 30);
  btnRemove.setText("Remove Textfield");
  btnRemove.addEventHandler(this, "remove_click");
}

```

---

<div class="post-metadata">

**Author:** ![Jos](https://avatars.discourse-cdn.com/v4/letter/j/2bfe46/32.png) [@Jos](https://discourse.processing.org/u/Jos)\
**Post date:** [September 8, 2021, 9:30am UTC](https://discourse.processing.org/t/how-to-delete-a-g4p-gtextfield/31897/3 "2021-09-08T09:30:26Z")

</div>

Thanks, I overlooked the member function setVisible().

```auto
import g4p_controls.*;

GTextField gtf;

void setup()
{ fill(200); size(400,200);
  fill(255); rect(300,0,100,100); rect(300,100,100,100);
  fill(0); text("create",320,50); text("delete",320,150);
  gtf=new GTextField(this,110,130,70,20); // text field
  gtf.tag="gtf";
  gtf.setText("hello");
  gtf.setFocus(true);
  gtf.setVisible(false);
}

void draw()
{
}

void mousePressed()
{ if(mouseX>300 && mouseX<400 && mouseY>0 && mouseY<100) // create
  { fill(100); rect(110,70,70,20); // draw rectangle
    gtf.setVisible(true); // show text field
  }
  if(mouseX>300 && mouseX<400 && mouseY>100 && mouseY<200) // delete
  { fill(200); rect(0,0,300,200); // remove rectangle
    gtf.setVisible(false); // hide text field
  }
}
 

```

Now it works fine.
