Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Text field key press...

Posted on 1999-07-15
5
Medium Priority
?
250 Views
Last Modified: 2010-04-16
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.
0
Comment
Question by:kryzb
[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
 
LVL 16

Expert Comment

by:heyhey_
ID: 1246097
you can try something like
textField.postEvent(new Event(textField, 8, "BACKSPACE"));

but I don't think that it wil work ...
0
 
LVL 8

Expert Comment

by:diakov
ID: 1246098
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
 
LVL 8

Expert Comment

by:diakov
ID: 1246099
This is jdk 1.1 solution, it may have to be adjusted for the  Java 2.
0
 

Author Comment

by:kryzb
ID: 1246100
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
 

Accepted Solution

by:
hinzer earned 150 total points
ID: 1246101
postEvent has been replaced by  dispatchEvent.
0

Featured Post

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

722 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