Vernstump
asked on
JTextField Text Validation
I am trying to catch the users input for binary validation.
Example:
1) The user will attempt to edit an existing binary string in a JTextField "00001111"
2) If the user enters any text other than a '1' or a '0', I want there input to be rejected and the original string remain in the TextField
3) If the user inputs a '1' or a '0'..it will replace the bit as requested
I have a Document listener on thh Field, limiting the amount of binary bits in my string.
I need help on Java Text Validation??
Thanks
Example:
1) The user will attempt to edit an existing binary string in a JTextField "00001111"
2) If the user enters any text other than a '1' or a '0', I want there input to be rejected and the original string remain in the TextField
3) If the user inputs a '1' or a '0'..it will replace the bit as requested
I have a Document listener on thh Field, limiting the amount of binary bits in my string.
I need help on Java Text Validation??
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Thanks for the points :)
package free.util;
import javax.swing.text.PlainDocu
import javax.swing.text.Attribute
import javax.swing.text.BadLocati
import javax.swing.text.AbstractD
import java.awt.Toolkit;
/**
* A PlainDocument extension which allows to disallow certain changes,
* by implementing the isChangeAccepted(String, String) method and returning
* false. A beep is emitted when an attempt to make an unacceptable change is
* made.
*/
public abstract class StrictPlainDocument extends PlainDocument{
public StrictPlainDocument(){
super();
}
protected StrictPlainDocument(Abstra
super(c);
}
/**
* Overrides PlainDocument.insertString
* check whether the change is accepted before applying the change.
*/
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException{
String oldString = getText(0,getLength());
String newString = new StringBuffer(oldString).in
if (isChangeAccepted(oldStrin
super.insertString(offs,st
else
Toolkit.getDefaultToolkit(
}
/**
* Returns true if the change from the old text to the new text is acceptable,
* returns false otherwise.
*/
public abstract boolean isChangeAccepted(String oldText, String newText);
}
package free.util;
/**
* A StrictPlainModel which only allows inputting an integer value in a certain
* range.
*/
public class IntegerStrictPlainDocument
/**
* The minimum allowed value.
*/
private final long minValue;
/**
* The maximum allowed value.
*/
private final long maxValue;
/**
* The radix of the input.
*/
private final int radix;
/**
* Creates a new IntegerStrictPlainDocument
* integer values in the given radix in the range [minValue..maxValue].
*
* @param minValue The minimum allowed value.
* @param maxValue The maximum allowed value.
* @param radix The radix of the input.
*/
public IntegerStrictPlainDocument
this.minValue = minValue;
this.maxValue = maxValue;
this.radix = radix;
}
/**
* Creates a new IntegerStrictPlainDocument
* integer values in the range [minValue..maxValue].
*
* @param minValue The minimum allowed value.
* @param maxValue The maximum allowed value.
*/
public IntegerStrictPlainDocument
this(minValue, maxValue, 10);
}
/**
* Returns true if the new text represents an integer in the correct range when
* parsed using the appropriate radix, returns false otherwise.
*/
public boolean isChangeAccepted(String oldText, String newText){
try{
long val = Long.parseLong(newText,get
if ((val<getMinValue())||(val
return false;
else
return true;
} catch (NumberFormatException e){
return false;
}
}
/**
* Returns the radix used by this IntegerStrictPlainDocument
*/
public int getRadix(){
return radix;
}
/**
* Returns the minimal allowed value.
*/
public long getMinValue(){
return minValue;
}
/**
* Returns the maximal allowed value.
*/
public long getMaxValue(){
return maxValue;
}
}
You can use IntegerStrictPlainDocument
Sasha Maryanovsky.