• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sun java tutorial swing addActionListener code

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I have a knack for fixing things like this ... um ... sorry ... here is a consilitory tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic