Hi Peter,
I started this as a new thread because the old one was really about filtering for data type.
Based on your helpful sketch, I’ve implemented the following to limit character entry to 3 in a textAREA:
public void textarea1_change1(GTextArea source, GEvent event) { //_CODE_:textarea1:904265:
println("textarea1 - GTextArea >> GEvent." + event + " @ " + millis());
String data = textarea1.getText();
String shorterData = "";
if (data.length() > 3) {
try {
shorterData = data.substring(0, data.length()-1);
textarea1.setText(shorterData);
textarea1.moveCaretTo(0, 3);
}
catch (Exception e){
//e.printStackTrace();
}
}
} //_CODE_:textarea1:904265:
This ALMOST works as I wish. But the cursor goes back to between the 2nd and 3rd characters - I’d like it to just remain at the end. Increasing the moveCaret to 4 does not work.
Also, similar code with a text FIELD totally crashes:
public void txf1Change(GTextField source, GEvent event) {
String data = txf1.getText();
String shorterData = "";
if (data != "") {
Validate.Integer(source);
//if (txf1.getText().length() > 3) txf1.setLocalColorScheme(GCScheme.SCHEME_10);
if (data.length() > 3) {
try {
shorterData = data.substring(0, data.length()-1);
txf1.setText(shorterData);
}
catch (Exception e){
//e.printStackTrace();
}
}
}
println("text: '" + txf1.getText() + "'");
}
Here is the total code, 99.9% of it yours from previous topic. Oh I should mention I made a custom color scheme 10 instead of the RED_SCHEME so it’s more aggressive looking.
Thanks for any insights. Wonderful library!
Mike
import g4p_controls.*;
GTextField txf1, txf2, txf3, txf4;
GTextArea textarea1;
public void setup() {
size(400, 400, JAVA2D);
createGUI();
}
public void draw() {
background(230, 230, 255);
}
public void txf1Change(GTextField source, GEvent event) {
String data = txf1.getText();
String shorterData = "";
if (data != "") {
Validate.Integer(source);
//if (txf1.getText().length() > 3) txf1.setLocalColorScheme(GCScheme.SCHEME_10);
if (data.length() > 3) {
try {
//txf1.appendText("\b");
shorterData = data.substring(0, data.length()-1);
//txf1.setText("");
txf1.setText(shorterData);
}
catch (Exception e){
//e.printStackTrace();
}
}
}
println("text: '" + txf1.getText() + "'");
}
public void txf2Change(GTextField source, GEvent event) {
Validate.Integer(source, -50, 100);
}
public void txf3Change(GTextField source, GEvent event) {
Validate.Float(source);
}
public void txf4Change(GTextField source, GEvent event) {
Validate.Float(source, - Float.MAX_VALUE, -Float.MIN_VALUE);
}
public void textarea1_change1(GTextArea source, GEvent event) { //_CODE_:textarea1:904265:
println("textarea1 - GTextArea >> GEvent." + event + " @ " + millis());
String data = textarea1.getText();
String shorterData = "";
if (data.length() > 3) {
try {
shorterData = data.substring(0, data.length()-1);
textarea1.setText(shorterData);
textarea1.moveCaretTo(0, 3);
}
catch (Exception e){
//e.printStackTrace();
}
}
} //_CODE_:textarea1:904265:
static class Validate {
// Additional validation methods can be added to suit your needs
public static Integer Integer(GTextField tf) {
tf.setLocalColorScheme(GCScheme.BLUE_SCHEME);
Integer n = null;
try {
n = Integer.parseInt(tf.getText());
}
catch(NumberFormatException nfe) {
tf.setLocalColorScheme(GCScheme.SCHEME_10);
}
return n;
}
public static Integer Integer(GTextField tf, int low, int high) {
Integer n = Validate.Integer(tf);
if (n != null && (n < low || n > high)) {
tf.setLocalColorScheme(GCScheme.SCHEME_10);
}
return n;
}
public static Float Float(GTextField tf) {
tf.setLocalColorScheme(GCScheme.BLUE_SCHEME);
Float n = null;
try {
n = Float.parseFloat(tf.getText());
}
catch(NumberFormatException nfe) {
tf.setLocalColorScheme(GCScheme.SCHEME_10);
}
return n;
}
public static Float Float(GTextField tf, float low, float high) {
Float n = Validate.Float(tf);
if (n != null && (n < low || n > high)) {
tf.setLocalColorScheme(GCScheme.SCHEME_10);
}
return n;
}
//public void Length (GTextField tf) {
// if (tf.getText().length() > 3) tf.setLocalColorScheme(GCScheme.SCHEME_10);
//}
}//end class validate
public void createGUI() {
G4P.messagesEnabled(false);
G4P.setGlobalColorScheme(GCScheme.BLUE_SCHEME);
G4P.setMouseOverEnabled(false);
G4P.setInputFont("Arial", G4P.PLAIN, 16);
surface.setTitle("Validating number input");
txf1 = new GTextField(this, 10, 20, 380, 30, G4P.SCROLLBARS_NONE);
txf1.setPromptText("Enter an integer");
txf1.setOpaque(true);
txf1.addEventHandler(this, "txf1Change");
txf2 = new GTextField(this, 10, 60, 380, 30, G4P.SCROLLBARS_NONE);
txf2.setPromptText("Enter integer -50 to 100 incl");
txf2.setOpaque(true);
txf2.addEventHandler(this, "txf2Change");
txf3 = new GTextField(this, 10, 100, 380, 30, G4P.SCROLLBARS_NONE);
txf3.setPromptText("Enter any decimal number");
txf3.setOpaque(true);
txf3.addEventHandler(this, "txf3Change");
txf4 = new GTextField(this, 10, 140, 380, 30, G4P.SCROLLBARS_NONE);
txf4.setPromptText("Enter any negative number");
txf4.setOpaque(true);
txf4.addEventHandler(this, "txf4Change");
textarea1 = new GTextArea(this, 11, 313, 372, 80, G4P.SCROLLBARS_NONE);
textarea1.setOpaque(true);
textarea1.addEventHandler(this, "textarea1_change1");
}