Solved

How to move the cursor back to a text field ?

Posted on 2000-04-02
5
248 Views
Last Modified: 2012-05-04
I am using Java Swing .I have a dialog box in which there are three JTextFields. A user can enter some data in a text field and I validate that data before user moves to the next field by pressing "TAB" or using mouse to point to the next field. I have registered focusListener on these three JTextFields and as the focus is lost from one field, i call my method x() to validate the data entered by user. If the data is wrong, a message window is displayed showing to the user what was wrong in the data entered. Now my problem is I call method x() in focusLost(e) method and even if there is an error message, the cursor moves to the next field which I don't want. I want the cursor to be placed back to the textfield whose validation generated the error so that the user can correct it and if it is correct, then only cursor should move to the next textfield. Immediate help will be appreciated.
0
Comment
Question by:vi_sharma
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 

Expert Comment

by:refactor
ID: 2677666
Use requestFocus() method?
0
 

Author Comment

by:vi_sharma
ID: 2677686
Can you explain in detail how to use it? I tried it..but it's not working..
0
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 2677766
You can also listen to focusGet events and when the textfield that should get focus gets the focus execute "previousTextField.requestFocus()" which would give the focus to the "previousTextField" text field.
0
 
LVL 1

Expert Comment

by:sankars98
ID: 2680537

 You can use SwingUtilities.invokeLater method for requestFocus call.


public void focusLost( FocusEvent e ){

   // blah blah blah .....

  if( error ){
  SwingUtilities.invokeLater( new Runnable(){
     public void run(){
           textField1.requestFocus();
     }

});
  }
}

 This will solve your problem...

Sankar S.
0
 
LVL 2

Accepted Solution

by:
anand29 earned 100 total points
ID: 2687685
When you lost the focus that time storer that component into a temprary variable and when you validate the data in your methodx(),and if the data is wrong that time u can requestFocus() for that stored component this way yur focus will remail at same component.

And that stored component is nothing but the same component from where you lost the focus previously.

hope this will help u...
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question