Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

KeyListener problems - wont' work?

 
Gunslinger
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I can't get this keylistener working... my jTextField does not change when I press keys while jButton1 has focus. Can anyone tell me what the heck I'm doing wrong? I've tried many different solutions, but nothing has worked.
 
James Brooks
Gunslinger
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm, I used the NetBeans interface (right-click -> events -> keyreleased), and coded inside the method it created for me, and it works. Wonder what I was doing wrong? Oh, well...
 
Rancher
Posts: 43016
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks a bit as if you're adding the key listener before the components get initialized - try doing it afterwards.
 
James Brooks
Gunslinger
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:It looks a bit as if you're adding the key listener before the components get initialized - try doing it afterwards.



You're saying, before my JButton and JTextFields are initialized?
 
Ulf Dittmer
Rancher
Posts: 43016
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, afterwards. It's not clear what's going on in the code because just about everything related to creating the GUI is missing, but I'm assuming that the "initComponents" method initializes the GUI. Note how the event listener is added before that (which also means that "jButton1" must have been created even before that, or you would have gotten a NullPointerException).
 
James Brooks
Gunslinger
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:No, afterwards. It's not clear what's going on in the code because just about everything related to creating the GUI is missing, but I'm assuming that the "initComponents" method initializes the GUI. Note how the event listener is added before that (which also means that "jButton1" must have been created even before that, or you would have gotten a NullPointerException).



Ah, OK, got it. Thank you!
 
The City calls upon her steadfast protectors. Now for a tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic