views:

38

answers:

1

I've just managed to get KeyListener to work in an empty application, but then I tried to implement it in my application and it simply doesn't work, no matter how much I enter keys!

A friend told me it's because I have buttons (JButton) on the application (and I implement the actionPerformed method,) can anyone explain to me why this is happening (and how to fix it) ?

EDIT:- Yes, the problem is about focus, and I found the solution in some forums, and the solution is very simple. It`s by adding: setFocusable(true); after, setVisible(true); in the class that extends JFrame.

A: 

To wich component are you adding the KeyListener? i think that if you want to listen the KeyEvents in the whole calculator you must add it to the Container in wich the buttons are.

But i belive that only the component who has the focus recive the KeyEvents, so that i dont know if the container can has the focus or if the events are promoted to their parent if the focus is on the buttons of the calculator.

You can solve this adding a keyListener to the container doing the stuff (printing the characters i suposse), and to the buttons and the textfield, promoting the event to it's container

Telcontar