Text field key press...

Can I simulate the press of a key in a JTextField programatically?

When my screen first draws, I want my program to press the 'cursor back' button in a certain field.  What code do I need to do this?

Cheers.

Kryz.
kryzbAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

heyhey_Commented:
you can try something like
textField.postEvent(new Event(textField, 8, "BACKSPACE"));

but I don't think that it wil work ...
0
diakovCommented:
I have a more specific suggestion:

EventQueue q = this.getToolkit().getSystemEventQueue();

//play some events
long time =  System.currentTimeMillis();
KeyEvent E1 = new KeyEvent((Component)ttt, KeyEvent.KEY_PRESSED, time - 7 , 0, KeyEvent.VK_A, 'a'); KeyEvent E2 = new KeyEvent((Component)ttt, KeyEvent.KEY_RELEASED, time - 6, 0, KeyEvent.VK_A, 'a'); KeyEvent E3 = new KeyEvent((Component)ttt, KeyEvent.KEY_TYPED, time - 5, 0, KeyEvent.VK_UNDEFINED, 'a');
                                    q.postEvent(E1);
q.postEvent(E2);
q.postEvent(E3);

-------
Each key event consists of 3 simple events. you have to post sequences in order to mimic a normal typing behaviour. The typing will occur whereever the focus is, e.g. your textfield.

Cheers,
  Nik
0
diakovCommented:
This is jdk 1.1 solution, it may have to be adjusted for the  Java 2.
0
kryzbAuthor Commented:
As you mentioned, this is the 1.1 solution.  Do you know what the JDK 1.2 version of 'postEvent' is, as I keep getting depricated warnings?

Cheers.

Kryz.
0
hinzerCommented:
postEvent has been replaced by  dispatchEvent.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.