This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes keylistener in a custom component Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "keylistener in a custom component" Watch "keylistener in a custom component" New topic
Author

keylistener in a custom component

miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
in eclipse i wrote this class:

when i run it,the label indeed reacts to capslock on and off, but then i added this panel to my main gui:

and now, from my gui, nothing happens

what am i missing?

TiA


java amateur
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
but then i added this panel to my main gui...nothing happens
The KeyListener works if the component it is listening to has the focus. If there are other
components in your gui/JFrame such as AbstractButtons or TextComponents they will naturally
take the focus and your JPanel will not receive the focus unless you can navigate to it.
One way to get around this is to bind KeyStrokes to the JPanel which will work even if it is
not the current focus owner. The JComponent method getInputMap(int condition) allows
you to specify the selection behavior with regard to focus in the gui.
For details and background info see How to Use Key Bindings.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
One way to get around this is to bind KeyStrokes to the JPanel which will work even if it is
not the current focus owner. The JComponent method getInputMap(int condition) allows
you to specify the selection behavior with regard to focus in the gui.

thanks for the help, in first place
i'm still frustrated because i tried:

i tried with
rodape.WHEN_FOCUSED also

and still nothing happens

can you please give me another hint?

TiA

EDIT
is that the right way to write: CapsLock, or Caps Lock??
[ May 05, 2007: Message edited by: miguel lisboa ]
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
In the Method Detail for the KeyStroke.getKeyStroke(String s) method it explains how
to determine what goes in the argument: it's the part after "VK_" (which we find) in the
KeyEvent Field Summary Section for the desired int. Therefore, I would try
Edit: formatting.
[ May 05, 2007: Message edited by: Craig Wood ]
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
still no luck, with:

i tried also with
[ May 05, 2007: Message edited by: miguel lisboa ]
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
i just read your code (no time to run it) and you inserted an actiob method; indeed i thought about it yesterdat, but i said to myself: if i write that code, means i have to write the tpogle code (enabled true/false for the label) twice: once in my custom panel and again in my main gui, so i discarted the idea
anyway i'm with a terrible cold, i just got out of bed to take my medicine and later i'll be back

thanks a lot, craig!
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
one class is worth one thousand words
ty craig

now i feel much better; here's what i've done
my custom component is now a dumb class - no logic in it
in my main gui, as i want to reuse my component, i have:

i was getting so frustrated that i even comtemplated the idea of writing the very same dumb code for the labels in my custom panel and insert it each time i needed in every tab where i wanted it

thanks once more craig
 
Don't get me started about those stupid light bulbs.
 
subject: keylistener in a custom component
 
Similar Threads
refresh label
TicTacToe - Computer's move
Caps Action problem
How can I change a .jpg icon image of an icon for a jLabel using Eclipse IDE?
Gap between JSeparators