• 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

proper use of keyListener

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am given an assignment to make a calculator by my teacher... i have done most of the work but since he did not teach us the use of keyListener i am not able to use it.


Even a little help would be much obliged.
i apollogise for my bad english
thank u.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I think you are going about the whole exercise the wrong way, I am afraid. You appear to be starting from a GUI and trying to get that to calculate, which I think is the wrong way to go about it.
How would you create a calculator without a GUI? Get that working, so you can call add(123, 456) or divide(123.45, 678.9) and get it to return an answer. Only then should you think of putting a GUI atop it.

Why on earth are you using Frame, or the show() method? It would appear you are learning programming practices which are 15 years out of date. Look up Component#show() and see what it says about it.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:It would appear you are learning programming practices which are 15 years out of date.



I think that would include the idea of using a KeyListener, too. If you're making a GUI calculator with buttons for the digits 0-9 and the operators +, * and so on, then all you should need is to use ActionListeners to respond to clicks on those buttons. There shouldn't be any need to listen for key clicks. (Especially if this is a problem for beginners.)
 
sami tariq
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
to sir campbell ritchie.
im sure ure right about everything but i have to make the calculator only using the topics we have discused in the class and Frame is the only topic taught to us for making a GUI program.


to sir paul clapham.
but my teacher is also asking that the calculations should also be done when the operator is pressed on the keyboard, therefore i looked into keyListener but cant get it to work.

Thank u.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, usually when I want to use a feature of Java, and I don't know anything about that feature, I look for a tutorial about it on the Internet. The web search keywords I use are "java X tutorial" where X is the feature in question.

So if you are stuck with those requirements, your web search keywords would be java key listener tutorial.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have added the key listener to the TextField which is fine but it means the keyListener will only get called if the TextField has focus when a key on the keyboard is pressed.
It's a long time since I used AWT components and I'm not sure if/how you can add a keyboard listener at the container level. One simple (albeit inelegant) option you have is to set the focus back to the TextField after every button is pressed and that way you will always get the keyboard events.
 
sami tariq
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you tony i have got it to work using requestFocusInWindow() method after every actionEvent then used the getKeyChar() method to read the + - * / key stroke, so thank you for the focus tip.

thank you all of you for your help.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote: . . .
It's a long time since I used AWT components . . .

And nobody else uses them nowadays.

set the focus back to the TextField . . .

That return of focus looks about the only elegant bit of programming in this whole thread.

Knowing what I know now, if I personally were in OP’s position, I personally would be looking for a different course.
 
It's just a flesh wound! Or a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic