Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

KeyEvents

 
Donald Hume
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps key bindings are a better idea.
 
Donald Hume
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here's a simple keybindings example
click the buttons, or press 1 or 2

 
Donald Hume
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic