Does anyone know if it is possible to add a key listener to an object that is not a Component or a subclass of a component?
I would like to add a key listener to a self defined class, but I'm not sure it can be done.
Thanks,
Jams
Printable View
Does anyone know if it is possible to add a key listener to an object that is not a Component or a subclass of a component?
I would like to add a key listener to a self defined class, but I'm not sure it can be done.
Thanks,
Jams
Yes you can add code to you class for a listener. What class does your class extend?
If you don't extend any Swing class that has a listener, then your class will have to write ALL the code to support the listeners. If that is the exercise you are trying to do, then it can be done. You'll have to convince the JVM that your class can get the focus and to have the JVM give your class the focus.
If you don't want to write all the code needed to support a listener, then extend a class that does support the listener.
I'm creating a class to represent a player in a very small game. The Player class extends Object and has a whole slew of functionality in the works. Originally, I created a Controller class that implemented KeyListener and was activated by a few characters. The Controller class, on key press, began a switch statement that would check for characters, w, a, s, and d. Depending which character was pressed, the appropriate player method was called. I have a third class that I call Canvas. It is a self repainting JPanel that will run at a defined fps. The canvas is using Controller as a key listener. This worked GREAT considering it was a rapid prototype.
However, I would much rather prefer the Player class be independent of two other classes. Right now the canvas receives a key and passes it to the controller which tells the player which way to move. The player has private data to keep track of it's location and it's drawToGraphics() method uses these data members when displaying.
Any advice on where to start?
Thanks,
Jams
Please explain?Quote:
I would much rather prefer the Player class be independent of two other classes.
Unless you are interested in learning how to write your own Swing classes framework, you'll be better off extending an existing class that does much of the GUI work for you.
Well, the Player class is what I would like to add these KeyListeners to. That way the The Player receives a KeyEvent and respond accordingly.
Right now the Canvas has a KeyListener, Controller. Controller implements KeyListener and it's constructor requires a Player. So, when a key is pressed, the Controller calls the appropriate function on the Player that was passed in the constructor.
I would like to add the key listener to the Player class so that it will not have to rely on the Canvas to pass input to the Controller to make the the corresponding player move. I would like to have the player have a listener to eliminate the need for the separate controller class to move the player.
Thank you for your help Norm. I have it working in a more manageable way now!