Solved

How to move the cursor back to a text field ?

Posted on 2000-04-02
5
243 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
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse package explorer vs project explorer view 2 126
how do i compare an object based on two fields 6 53
replace a word with other 1 44
Android development question 2 36
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.

776 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