• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sun java tutorial swing addActionListener code

 
Tom Never
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am reading the sun tutorial and I don't see in this code where the 2nd addActionListener is in the code for both the enter and button. They write in the tutorial:

We want to perform the conversion when the user clicks the button or presses Enter in the text field. To do so, we add an action event listener to the convertTemp button and tempCelsius text field.

convertTemp.addActionListener(this);
tempCelsius.addActionListener(this);
...


But I don't see it in the code. It works, for both button and enter, but where is it in the code? Thanks, Tom

I]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're not going crazy; the writeup doesn't match the code. In the code, they only add the event handler to the button, not to the JTextField. But they also do this:

converterFrame.getRootPane().setDefaultButton(convertTemp);

That ensures that any unhandled "Enter" presses will filter up to the RootPane, which will dispatch them as ActionEvents to the given default button. Note how when you press Enter, the button flashes. So the JTextField isn't handling the Enter presses at all!

Maybe they wrote it one way, changed it, and didn't update the description!
 
Tom Never
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I'm still learning my way around packages/api documentation. It's amazing what they don't tell you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic