Solved

How to move the cursor back to a text field ?

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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:
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…

709 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now