?
Solved

How to move the cursor back to a text field ?

Posted on 2000-04-02
5
Medium Priority
?
261 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 200 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month16 days, 4 hours left to enroll

850 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