• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Using Key Bindings in JDialog

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to take some action on my JDialog when the space bar is pressed. After reading the documentation it seems like I there should be methods named "getInputMap" and "getActionMap" to do the binding. However, they do not seem to exist for a JDialog object. How to I accomplish this using JDialog. TIA.

 
Saloon Keeper
Posts: 3291
146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Sam,

if d is your dialog, you can use the input- and actionmap as follows:
 
Rancher
Posts: 3051
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe you need to use:



The root pane doesn't have focus so you can't use the default InputMap.
 
Sam Ritter
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. That got me over the syntax issue but it does not always detect the space bar. I need to play with that a bit to see what is wrong.
 
Sam Ritter
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quick question. Does it matter where in the code the getInputMap is placed? That is before/after setVisible?
 
Rob Camick
Rancher
Posts: 3051
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

but it does not always detect the space bar



Maybe another component that has focus handles the space bar.

Post your SSCCE demonstrating the problem.

Does it matter where in the code the getInputMap is placed?



The bindings just need to be in place before the user interacts with the GUI.
 
Sam Ritter
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I guess I will have to develop one. There are only 2 windows open. The main JFrame and this JDialog.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!