Jos
August 24, 2021, 8:17pm
1
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?
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:
}
}
quark
September 6, 2021, 6:38am
2
@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.
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");
}
Jos
September 8, 2021, 9:30am
3
Thanks, I overlooked the member function setVisible().
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.