Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Applets and the fly likes KeyListener problems - wont' work? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "KeyListener problems - wont Watch "KeyListener problems - wont New topic
Author

KeyListener problems - wont' work?

James Brooks
Gunslinger
Ranch Hand

Joined: Aug 17, 2006
Posts: 165
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.


Hello. My name is Inigo Montoya. You killed my father. Prepare to die.
James Brooks
Gunslinger
Ranch Hand

Joined: Aug 17, 2006
Posts: 165
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...
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39529
    
  27
It looks a bit as if you're adding the key listener before the components get initialized - try doing it afterwards.


Ping & DNS - updated with new look and Ping home screen widget
James Brooks
Gunslinger
Ranch Hand

Joined: Aug 17, 2006
Posts: 165
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
Marshal

Joined: Mar 22, 2005
Posts: 39529
    
  27
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
Ranch Hand

Joined: Aug 17, 2006
Posts: 165
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: KeyListener problems - wont' work?
 
Similar Threads
Applet not initialising
Passing Variable Beetween GUI's
JLabel1.setText error
How do I control the size of a button (or any component) in an applet or Frame?
Getting an a TextBox to show in an Applet