File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes How to crate KeyListener in different Thread? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to crate KeyListener in different Thread?" Watch "How to crate KeyListener in different Thread?" New topic
Author

How to crate KeyListener in different Thread?

Rohit Kumar
Ranch Hand

Joined: Sep 20, 2007
Posts: 53
Hi,

I have a class which implements KeyListener and in keyPressed() chek whether a a particular key pressed or not?

In same class i have to take a decision on the basis of whether that particular key is pressed or not?

But i found that control comes to keyPressed() after the decision is taken.

I used sleep(), but still the control comes later at keyPRessed().

How to do it in differnt Thred, so that keyPressed() will execute first and then that decision?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

Originally posted by Rohit Kumar:
Hi,

I have a class which implements KeyListener and in keyPressed() chek whether a a particular key pressed or not?

In same class i have to take a decision on the basis of whether that particular key is pressed or not?

But i found that control comes to keyPressed() after the decision is taken.

I used sleep(), but still the control comes later at keyPRessed().

How to do it in differnt Thred, so that keyPressed() will execute first and then that decision?


Do you really need to use threads? I think not.
Something like this should ensure the proper sequence you want.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Rohit Kumar
Ranch Hand

Joined: Sep 20, 2007
Posts: 53
The senario is something like this.
User gets a dialog box, and if he selects check box, he will not get this dialog box further.

Now suppose he has already selected the check box (so will not get dialog box), but want to get dialog back, so he can get it back by pressing a particular key.

Now while if user already selected 'not to show dialog' while showing dialog i check whether the key is pressed or not(so dialog can be show again).
But this condition is cheked first and then keyPressed event is fired (if pressed key).

Certanly i can't do this condition checking in keyPressed.
So i thought of another thread.

Any clue?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

I am confused. Is the dialog triggered through the same key?

In any case, my code snippet would still work.
In the processKeyEvent you toggle the boolean which indicates if the dialog should be shown or not.
In take decision, you check this boolean, and if required, popup the dialog.

Makes sense?

PS. On a side note, usually, the show dialog option is through some visual control like some options dialog which is triggered from the menu bar. Better usability that way.
Rohit Kumar
Ranch Hand

Joined: Sep 20, 2007
Posts: 53
Well if check box from the dialog is not set, then there is no any connnection of key and dialog.

The key event comes into the picture only if the check box is already set.

If the check box is already set, then normal procedure is not to show the dialog and procede further.

But if check box is set, and user want to see the dialog, he can see by pressing a particular key.

therefore,

a) if check box is not already set, dialog will always appear
b) if check box is set, dialog will not appear
c) if check box is set, and user still want to see dialog, he can get it by pressing a particular key.

So i am checking whether key is set or not as a condition before displaying dialog. But key event is fired after control comes to this key check condition.

So i was looking for new thread.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to crate KeyListener in different Thread?
 
Similar Threads
Button keyboard event
keyListener problems
Key Event listener question.
KeyListerner in JPanel
Key listener or what?