Win a copy of Pragmatic AI this week in the Artificial Intelligence 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Sun java tutorial swing addActionListener code  RSS feed

 
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]
 
author and iconoclast
Sheriff
Posts: 24220
40
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!