I will make the cancelSelection
method public for the next release. Just a warning though calling this method from inside a Processing key event handler e.r. keyTyped
will crash the sketch. Use another control e.g. a button to cancel selections in text fields and areas.
The easiest way is simply add the controls to a list then iterate over them like this-
import g4p_controls.*;
ArrayList<GTextField> listA = new ArrayList<GTextField>();
public void setup() {
size(400, 240, JAVA2D);
createGUI();
addToList(listA, txf1, txf2, txf3);
setRandomText(listA, 20, 30);
}
public void draw() {
background(230, 230, 255);
}
void setRandomText(ArrayList<GTextField> list, int minLength, int maxLength) {
for (GTextField field : list) {
int len = (int) random(minLength, maxLength);
int pos = (int) random(0, rawText.length() - len);
field.setText(rawText.substring(pos, pos+len));
}
}
void addToList(ArrayList<GTextField> list, GTextField... fields) {
for (GTextField field : fields) list.add(field);
}
String rawText = "Lorem Ipsum is simply dummy text of the "
+"printing and typesetting industry. Lorem Ipsum has been "
+"the industry's standard dummy text ever since the 1500s, "
+"when an unknown printer took a galley of type and "
+"scrambled it to make a type specimen book. It has "
+"survived not only five centuries, but also the leap into "
+"electronic typesetting, remaining essentially unchanged. "
+"It was popularised in the 1960s with the release of "
+"Letraset sheets containing Lorem Ipsum passages, and more "
+"recently with desktop publishing software like Aldus "
+"PageMaker including versions of Lorem Ipsum.";
public void randomIntput(GButton source, GEvent event) {
setRandomText(listA, 20, 30);
}
public void createGUI() {
G4P.messagesEnabled(false);
G4P.setGlobalColorScheme(GCScheme.BLUE_SCHEME);
G4P.setMouseOverEnabled(false);
G4P.setInputFont("Arial", G4P.PLAIN, 14);
surface.setTitle("Sketch Window");
txf1 = new GTextField(this, 20, 20, 370, 20, G4P.SCROLLBARS_NONE);
txf1.setOpaque(true);
txf2 = new GTextField(this, 20, 50, 370, 20, G4P.SCROLLBARS_NONE);
txf2.setOpaque(true);
txf3 = new GTextField(this, 20, 80, 370, 20, G4P.SCROLLBARS_NONE);
txf3.setOpaque(true);
btnRandom = new GButton(this, 220, 120, 170, 30);
btnRandom.setText("Randomise Text");
btnRandom.addEventHandler(this, "randomIntput");
}
GTextField txf1;
GTextField txf2;
GTextField txf3;
GButton btnRandom;