Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

KeyListener won't work on ALL computers  RSS feed

 
Andre Mercier
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I'm primarily a math teacher, but have recently started teaching AP Computer Science. I am very much a rookie when it comes to anything beyond the content of the Exam. Now that the exam is over, I'm trying to build some simple GUIs and simple games with the kids. I developed the classes below (along with a bunch more stuff) which work perfectly on my computer, but the Key Typing feature is not working on all my students computers (which is a hodge-podge of laptops). I've tried keyCodes instead of the Character method and that makes no difference. I've tried the code in both keyPressed and keyTyped as well.

I've read that Key Bindings is almost always a better strategy, but it seems quite a bit more confusing. It would take me a good while to sort it all out, so I can only imagine how most of my students would react to it. KeyListener seems much more manageable, but I cannot figure out why my current code won't work on everyone's computer. I'm sure I'm doing a variety of things that are not very sensible, but I'm mostly concerned with just getting it to function on everyone's computer. I'm open to all suggestions, but please try to keep in mind I am very very much a rookie to it all. Thanks for any help.





 
Rob Camick
Ranch Hand
Posts: 2752
11
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The basic code looks reasonable. The only thing I can suggest is that all GUI code should be executed on the Event Dispatch Thread (EDT). Read the section from the Swing tutorial on Concurrency, for more information.

Basically you can rename your main() method to createAndShowGUI. Then your new main() method would just include the code snippet from the above link.

I've read that Key Bindings is almost always a better strategy,


Yes it is for many reasons. First of all Swing was designed to be use with Actions. Actions are very powerful. At there simplest they are just an ActionListener because all you need to implement is the actionPerformed() method. However, they are very powerful because an Action can be shared. In you simple example the Action can be shared with the "R" KeyStroke. The next step in your course might be to create a "Reset" menu item. Well the Action can be shared by the menu item.

In addition to the above, when you use Key Bindings you can invoke the Action even when a component doesn't have focus.

Here is a simple example to get you started with Key Bindings.


Once you start adding support for more KeyEvents, your KeyListener will have a bunch of nested "if/else" statements, which should always be avoided when possible. As you can see the Action/Key Binding approach completely separates the logic.
 
Andre Mercier
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very helpful, thank you. The first suggestion works for me, hopefully that fixes the problem. You example of Key Bindings is by far the simplest I've seen so far, I'll look into that eventually too.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!