• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem in Event Handling  RSS feed

 
kirba devi
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Gui application :
i have a method like
public void keyPressed(KeyEvent Keyevent) {
component1.addkeyListener(new keyAdapter() {
if (Keyevent.getKeyCode() == KeyEvent.VK_RIGHT) {
//some operation invoked
} else if (Keyevent.getKeyCode() == KeyEvent.VK_LEFT) {
//some operation invoked
} else if (Keyevent.getKeyCode() == KeyEvent.VK_NUMPAD8) {
//some operation invoked
} else if (Keyevent.getKeyCode() == KeyEvent.VK_NUMPAD2) {
//some operation invoked
} else if (Keyevent.getKeyCode() == KeyEvent.VK_ENTER) {
//some operation invoked
} else if (Keyevent.getKeyCode() == KeyEvent.VK_ESCAPE) {
//some operation invoked
} else if (Keyevent.getKeyCode() == Keyevent.getKeyCode()) {
//some operation invoked
}
}

whenever user presses any key in this component1 ,keypressed method gets executed and based upon the if condition
corresponding operation is performed.what my problem is say for example pressing right key ,1st if condition is satisfied and
corresponding operation is going on.Immediately i am pressing right key again ,not even waiting for the
previous event to complete,and this causes some problem in my application and even crashes also.
i wan t to avoid.
i thought of putting synchronized keyword in keypressed method
ie public synchronized keypressed(KeyEvent Keyevent)
thinking that only one thread will we executing at a time.but my assumption fails.

Could someone clarify my doubt why it fails?is my assumption wrong?,and how to get rid of this problem
Its urgent!!!please help


Regards
Kirba
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless you are starting new threads, all the key events should be coming in on the Swing/AWT thread one after another, not trying to run simultaneously. Can you prove that the 2nd starts before the 1st finishes? Maybe just print some "method entry" and "method exit" messages with the thread name. Running on one thread would be consistent with synchronized making no difference.
 
kirba devi
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi James,
What you posted is correct,there is no point of putting synchronization .but in the previous message what i have posted see below..

what my problem is say for example pressing right key ,1st if condition is satisfied and
corresponding operation is going on.Immediately i am pressing right key again ,not even waiting for the
previous event to complete,and this causes some problem in my application and even crashes also.


How to avoid the problem?

Regards,
Kirba.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!