This is the code what i have typed. Its working but when i enter a value which is less then 0 and greater than 99, it still works, where as it shouldnt. Please help
public void focusLost(FocusEvent evt) {
if(evt.getSource() == mytextfield) {
System.out.println("Inside focuslost");
String text = mytextfield.getText();
if(text.startsWith("aa")) {
int value;
try {
value = Integer.parseInt(String.valueOf(text.charAt(text.length() - 1)));
}catch(NumberFormatException nfe) {JOptionPane.showMessageDialog(null,"textfield should be \n in the range of aa0 - aa99","Test",JOptionPane.ERROR_MESSAGE);return;}
if(value < 0 || value > 99) {
System.out.println("Inside if condition");
JOptionPane.showMessageDialog(null,"textfield should be \n in the range of aa0 - aa99","Test",JOptionPane.ERROR_MESSAGE);
mytextfield.transferFocus();
return;
}
}
else {
JOptionPane.showMessageDialog(null,"Invalid entry","Test",JOptionPane.ERROR_MESSAGE);
return;
}
}
}
Thanks for the tips. I have changed my code as follows
value = Integer.parseInt(String.valueOf(text.substring(text.lastIndexOf('a')+1,text.length())));
...
Also changed if condition to :
if(value < 0 && value > 99)
But still i get the same problem. If i enter values less 0 and greater than 99 its not entering if condition.
I have one more doubt. Is mytextfield.requestFocus() correct. Actually on error i want the cursor to go back to mytextfield. But it seems its not working. I dont see cursor blinking in mytextfield. I strictly dont want the cursor to move out of mytextfield untill i enter proper correct value. How can i do this? Please help me
> Imho, requestFocus() _should_ work. However, your
> code says "transferFocus()" -- are you sure about
> that?
Yeah i changed it to requestFocus(). And its keep displaying error message three times. Even if i move to other application i get error messages. How can i display error message only once and within that application only if i click on other other fields when value is wrong.
> Yeah i changed it to requestFocus(). And its keep
> displaying error message three times.
Make sure you didn't register your listener three times (we never know...)
> Even if i move to other application i get error messages.
> How can i display error message only once and within that
> application only if i click on other other fields
> when value is wrong.
From the [url=http://java.sun.com/javase/6/docs/api/java/awt/event/FocusEvent.html#isTemporary()]API doc[/url]:
There are two levels of focus events: permanent and temporary. Permanent focus change events occur when focus is directly moved from one Component to another, such as through a call to requestFocus() or as the user uses the TAB key to traverse Components. Temporary focus change events occur when focus is temporarily lost for a Component as the indirect result of another operation, such as Window deactivation or a Scrollbar drag. In this case, the original focus state will automatically be restored once that operation is finished, or, for the case of Window deactivation, when the Window is reactivated. Both permanent and temporary focus events are delivered using the FOCUS_GAINED and FOCUS_LOST event ids; the level may be distinguished in the event using the isTemporary() method.
Try passing your focus request to the event queue:
SwingUtilities.invokeLater(new Runnable() {
public void run() {
mytextfield.requestFocus();
}
});
> Try passing your focus request to the event queue:
> > SwingUtilities.invokeLater(new Runnable() {
>public void run() {
>mytextfield.requestFocus();
> }
> });
>
I didnt understand what you mean. You mean i have call invokeLater method in the place requestFocus() call.
> > Yeah i changed it to requestFocus(). And its keep
> > displaying error message three times.
>
> Make sure you didn't register your listener three
> times (we never know...)
>
No i have registered only once
> > Even if i move to other application i get error
> messages.
> > How can i display error message only once and
> within that
> > application only if i click on other other fields
> > when value is wrong.
>
> From the
> [url=http://java.sun.com/javase/6/docs/api/java/awt/ev
> ent/FocusEvent.html#isTemporary()]API doc[/url]:
> There are two levels of focus events: permanent and
> temporary. Permanent focus change events occur when
> focus is directly moved from one Component to
> another, such as through a call to requestFocus() or
> as the user uses the TAB key to traverse Components.
> Temporary focus change events occur when focus is
> temporarily lost for a Component as the indirect
> result of another operation, such as Window
> deactivation or a Scrollbar drag. In this case, the
> original focus state will automatically be restored
> once that operation is finished, or, for the case of
> Window deactivation, when the Window is reactivated.
> Both permanent and temporary focus events are
> delivered using the FOCUS_GAINED and FOCUS_LOST event
> ids; the level may be distinguished in the event
> using the isTemporary() method.
Ok i understood this. But how can this help me to solve my problem
> Ok i understood this. But how can this help me to solve my problem
Change
if(evt.getSource() == mytextfield) {
to
if ((evt.getSource() == mytextfield) && (!evt.isTemporary())) {
and
mytextfield.requestFocus();
to
SwingUtilities.invokeLater(new Runnable() {
public void run() {
mytextfield.requestFocus();
}
});
That should do it.