[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Text field key press...

Posted on 1999-07-15
5
Medium Priority
?
251 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
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

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

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

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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…
Suggested Courses
Course of the Month7 days, 17 hours left to enroll

607 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