wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Frame with Keybinders and focus Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Frame with Keybinders and focus" Watch "Frame with Keybinders and focus" New topic
Author

Frame with Keybinders and focus

Adrian Lee
Ranch Hand

Joined: May 10, 2007
Posts: 71
Hi everyone. I'm trying to use keybinding for my frame. I'm starting with press down button. However, the focus doesn't go to the next button. Is my program wrong? Can anyone advice me on where I went wrong?

My program code is pasted below.


Thanks for your time.

Best Regards,
Adrian Lee
[ May 13, 2007: Message edited by: Adrian Lee ]
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Try a different condition:

Although this works okay on the contentPane you can get a more general affect if you register it on the JRootPane instead. See JRootPane api comments.
Adrian Lee
Ranch Hand

Joined: May 10, 2007
Posts: 71
Thanks a lot Craig. I will try that out.
Adrian Lee
Ranch Hand

Joined: May 10, 2007
Posts: 71
Hi everyone, I've got a question.

I have 2 Frames, a mainframe & a subframe. My mainframe window responds to a button and opens up a new frame(subframe) based on "arrow" keypress and "enter" key. I would like to have key bindings at the subframe window which have a different layout. Therefore, I've tried inserting key bindings in both class and running them. However, that doesn't seem to work. Is there something that I need to change?

The codes that I've used in my mainframe to open a new class(subframe) is shown below.



Thanks in advance and taking time to reply me.

Adrian Lee
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Use the down key to navigate to the Entertainment button and press the space bar. Use the up
key to change the color in the dialog.
In general we try to have only one JFrame at a time and use dialogs for secondary top–level
containers.
There are three conditions that you can try to find what works for the InputMaps. These are
found in the JComponent api Method Detail for the getInputMap(int condition) method.
Adrian Lee
Ranch Hand

Joined: May 10, 2007
Posts: 71
Once again, thanks Craig!
Adrian Lee
Ranch Hand

Joined: May 10, 2007
Posts: 71
Hi again Craig. Sorry to trouble you.

I've been testing out the new frame and trying a few ways to go around it.

It seems that I can only use WHEN_IN_FOCUSED_WINDOW instead of
WHEN_ANCESTOR_OF_FOCUSED_COMPONENT for the new frame with buttons.
From there onwards, the frame goes back to the 1st problem that I've encountered earlier on. (When I press Up, Focus will get stuck on the next button and program will halt.) Is there another way to solve this?

Another question that I would like to ask is : Is it possible to open a few dialog boxes? As in JDialog1 -> JDialog2 -> JDialog3

Please advice on what I could do, Thanks Alot

Attached below is 2nd frame with buttons



Thanks again
Adrian Lee
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
It looks like you are designing a keyboard navigation system for your app. The key binding
approach, which may be workable, seems unwieldy for this. I would consider using the
default Swing setup which is keyboard navigation with the tab key in combination with a
FocusTraversalPolicy. This is discussed with examples in How to Use the Focus Subsystem.
See sections "Customizing Focus Traversal" and "Tracking Focus Changes to Multiple
Components". The intro shows how to set up custom focus traversal keys if the tab key is
unsuitable.
Is it possible to open a few dialog boxes? As in JDialog1 -> JDialog2 -> JDialog3
Yes. Make sure they are non–modal or they will freeze everything up.
Adrian Lee
Ranch Hand

Joined: May 10, 2007
Posts: 71
Thanks for answering me, Craig.
 
wood burning stoves
 
subject: Frame with Keybinders and focus