aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes KeyEvents Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "KeyEvents" Watch "KeyEvents" New topic
Author

KeyEvents

Donald Hume
Greenhorn

Joined: Aug 04, 2008
Posts: 12
Hey folks,

I'm trying to create a simple GUI based app and I'd like to be able to have key functionality.

I've tried messing around with it a bit, but I'm an ameature with EventHandlers and KeyListeners are brand spankin' new. Could someone give me an example of how to use a keylistener?

I know some important things are..
setMnemonic, (though I'm not sure of the proper usage)

creating a buttonlistener that implements KeyListener

Maybe like that? ^^

But other than that I'm lost. Any help, even just pointing me in the right direction would be greatly appreciated.

Much thanks
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
keylisteners need to be added to a component, and that component needs to have the focus

bookmark this page
http://java.sun.com/docs/books/tutorial/uiswing/TOC.html
and scroll down to 'writing event listeners', then to
'how to write a keylistener'

note: keylisteners are often the wrong approach, keybindings are better
Donald Hume
Greenhorn

Joined: Aug 04, 2008
Posts: 12
I'm having a grid of buttons. ie calculator. Can i just set focus for the whole panel? How would I handle that?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

Perhaps key bindings are a better idea.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Donald Hume
Greenhorn

Joined: Aug 04, 2008
Posts: 12
Alright,

I've done some research.

Here is my hierarchy for my GUI

JFrame
JPanel
JPanel1,JPanel2
JButton (many).

I'm trying to set KeyListeners. Here is how I've tried to implement this.

I have:



I've tried setting focusable on the main panel, the subpanels and the buttons.. (adding the keyListeners to each as well) but it just isn't working.

I've read the API's.. and I'm just not finding them helpful for my situation. If anyone could lend a pointer / some help it'd be much appreciated.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> I've read the API's..

musta missed this bit :-)

"For key pressed and key released events, the getKeyCode method returns the
event's keyCode. For key typed events, the getKeyCode method always returns
VK_UNDEFINED."

so, use keyPressed() instead.

a lot depends on how you're output is displayed - JLabel, JTextField etc

if a textfield, perhaps having the textfield as the only focusable component
will allow you to add the keyListener to only that component (buttons will
still work for actionListeners etc)

don't try and build it all at once, get the basics working - display, then
listener/s, then you can work on only numbers and addition/subtraction etc
Donald Hume
Greenhorn

Joined: Aug 04, 2008
Posts: 12
I gave what you said a shot, setting only the JTextField to focusable, and adding the listener to there, but no dice.

and for your information...

I have the whole calculator working. All the ActionListeners and functionality of the calculator have been working. This is the last step and I've spent more time fiddling around with it than all the other coding. It's just a bit frustrating. Any other ideas? Don't be shy.

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
here's a simple keybindings example
click the buttons, or press 1 or 2

Donald Hume
Greenhorn

Joined: Aug 04, 2008
Posts: 12
Michael Dunn,

Your first response about setting everything to setFocusable(false) except the main panel worked.

Great idea, I just didn't understand you completely at first.

Much appreciated
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: KeyEvents