Swing is UnUsable due to validation problem.
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Gokul_Kumar
Posted On:   Monday, December 10, 2001 12:19 AM

Hi, I have an application with four JTextFields, a JComboBox and two buttons( Save & Cancel ) in panel which is attached to the frame. I have a menubar attached to the frame as well. I need to call a validation function as soon as the user finished typing(Pressing Enter key or Tab ) the values in the JTextFields. But these validation function shouldn't be called when i click the menu or cancel button. Problem Statement: Where should i call this validation function? Problem 1: If i call this function in the focuslost methods of the JTextFields then validation function will be called when i click the menu. Problem 2: i have written the validation in focus lost of JTextFi   More>>

Hi,

I have an application with four JTextFields, a JComboBox and two buttons( Save & Cancel ) in panel which is attached to the frame. I have a menubar attached to the frame as well.

I need to call a validation function as soon as the user finished typing(Pressing Enter key or Tab ) the values in the JTextFields. But these validation function shouldn't be called when i click the menu or cancel button.

Problem Statement:

Where should i call this validation function?

Problem 1:
If i call this function in the focuslost methods of the JTextFields then validation function will be called when i click the menu.

Problem 2:
i have written the validation in focus lost of JTextField.
This causes problem when i click on the jtable or jcombobox. When i click on the combobox after entering a wrong value in the textfield, cursor remains in the jtextfield but i am able selected a value from the jcombobox as well.

The same problem persists when i call the validation function in the InputVerifier.

I cann't call the validation function in the JTextfields model(i.e PlainDocument class ) because i cann't check for a range or for a null value. Initially JTextfield have empty value and if i press tab then insertstring ( ) method
will not be called. So i maynot be possible to check for null values.

Following is the code of my validation function in the focus lost:

private void jTextField1_validationPerformed(FocusEvent e)
{
jTextField1.removeFocusListener(this);
int intValue = new Integer(jTextField1.getText ()).intValue();
if (intValue > 100)
{
JOptionPane.showMessageDialog(this, "Should be < 100");
jTextField1.requestFocus();
}
jTextField1.addFocusListener(this);
}

Following is the code of my validation function in the insertstring( ) method in PlainDocument which i have overwritten:

if( str != null)
{
char[] source = str.toCharArray();
char[] result = new char[source.length];
int j = 0;
for (int i = 0; i < result.length; i++)
{
if (Character.isDigit(source))
result[j++] = source;
else
Toolkit.getDefaultToolkit().beep();
}
str = new String(result, 0, j);
}
super.insertString(offs,str, a);

with regards
Gokul.

   <<Less
About | Sitemap | Contact