By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,824 Members | 1,247 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,824 IT Pros & Developers. It's quick & easy.

Can't get KeyListener to work

P: 42
I've done KeyListener before successfully, but for some reason I can't get it to work in my recent project. Here is the code:

View class (setBattleController is called upon creating new Battle object):

Expand|Select|Wrap|Line Numbers
  1. public void setBattleController(Battle b){
  2.         this.addKeyListener(b);
  3.         this.setFocusable(true);
  4.         this.requestFocus();
  5.     }
  6.     public void removeBattleController(Battle b){
  7.         this.removeKeyListener(b);
  8.     }
Battle (there is another controller for view, but it doesn't use keylistener)

Expand|Select|Wrap|Line Numbers
  1. public void keyTyped(KeyEvent ke){
  2.                //bunch of code
  3.         System.out.println("Here we are");
  4. }
Jul 23 '09 #1
Share this Question
Share on Google+
3 Replies

P: 42
Any help? It doesn't just seem to do anything.
Jul 23 '09 #2

P: 13,264
Put the println before the bunch of code to see if listener is called at all.
You might have have to give more information.
Jul 24 '09 #3

P: 42
I got it to work after regenerating serialUIDs for both classes, though I'm not yet finished with code. I'm bit confused why my middlemouse-button is not working with my current program in current system as it did with my laptop.

Any ways to get focus on JPanel for getting keyevents? (Without using mouselistener for focusing)
Jul 24 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.