JavaUser Code: Validating Float Values in JTextfield

User Code: Validating Float Values in JTextfield

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Here is some cool code for validating Float values in JTextfield. The code
offered below will validate user-entered values.
It will allow only one decimal point, digits from 0 to 9, and the Backspace key. It
won’t allow any other characters to be typed in.

If you are interested, you can use this in your work. Create an instance of
this class and pass the JTextField as a parameter to its constructor. It
will take from there. Just sit back and relax.

Note: Don’t hesitate to pass on your wild comments and imaginative
suggestions to me.


/**
 * Insert the type's description here.
 * Creation date: (3/15/01 9:50:16 AM)
 * @author: Chowdary K
 */
public class FloatKeyValidity implements java.awt.event.KeyListener
{
	private javax.swing.JTextField floatField;
	private int flag = 0;
	private int bkSpace = 0;
/**
 * FloatKeyValidity constructor comment.
 */
public FloatKeyValidity() {
	super();
}
/**
 * FloatKeyValidity constructor comment.
 */
public FloatKeyValidity(javax.swing.JTextField jtf)
{
	super();
	floatField = jtf;

}
/**
 * Insert the method's description here.
 * Creation date: (3/15/01 9:51:56 AM)
 * @return javax.swing.JTextField
 */
public javax.swing.JTextField getFloatField() {
	return floatField;
}
/**
 * Insert the method's description here.
 * Creation date: (3/14/01 10:37:57 AM)
 * @return boolean
 * @param str java.lang.String
 */
public boolean isDot(String str)
{
	int val = 0;
	for (int i = 0; i < str.length(); i++)
	{
		if (str.charAt(i) == '.')
		{
			val = 1;
			break;
		}
	}
	if (val == 0)
		return false;
	else
		return true;
}
/**
 * keyPressed method comment.
 */
public void keyPressed(java.awt.event.KeyEvent e)
{
	if(e.getKeyCode()==8)
	{
		bkSpace = 1;
	}
}
/**
 * keyReleased method comment.
 */
public void keyReleased(java.awt.event.KeyEvent e)
{
	bkSpace =0;
}
/**
 * keyTyped method comment.
 */
public void keyTyped(java.awt.event.KeyEvent ke)
{
	char c = ke.getKeyChar();
	String curVal = floatField.getText().trim();
	// If '.' is encountered for the first time, the flag is incremented
by 2.
	if (c == '.')
		flag = flag + 2;
	Character cc = new Character(c);
	// Get the Numeric Value from the Character and checking the
condition for flag to be 1.
	if ((cc.getNumericValue(c) == -1) && (flag > 1))
	{
		// Pass the value of the text as a String to the method
isDot and if true (dot is already there),
		// if it is false (dot is not there) set the value back to 0
to allow the user to enter dot again.
		if (!(isDot(curVal)))
			flag = 0;
	}
	// This will allow the user to enter the numbers as well as dot.
	if (!(((c >= '0' && c <= '9')) || (c == '.') || bkSpace==1))
		ke.consume();
	else
	{
		// If the flag is greater than 1 then the dot is not
allowed.
		if (flag > 1)
		{
			if ((c == '.'))
				ke.consume();
		}
	}
}
/**
 * Insert the method's description here.
 * Creation date: (3/14/01 10:41:14 AM)
 * @param newFlag int
 */
public void setFlag(int newFlag) {
	flag = newFlag;
}
/**
 * Insert the method's description here.
 * Creation date: (3/15/01 9:51:56 AM)
 * @param newFloatField javax.swing.JTextField
 */
public void setFloatField(javax.swing.JTextField newFloatField) {
	floatField = newFloatField;
}
}

About the Author

K.B.M.K. Chowdary is a software engineer specializing in Java technology.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories