• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with Key Bindings And JDialog

 
Rosie Fairfield
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am having a problem using key bindings with a JDialog. I want to toggle the JDialog to show and dispose (disappear) on alternate clicks of a hot key / bound key, small d.
I have a JPanel with a JButton handled by an ActionListener. The JButton correctly shows and disposes of the JDialog on alternate clicks, so I know that my JDialog and the toggling
function are working properly.
The problem is with the hot key: when I press the d key, the JDialog shows and the System.out statement in HotKeys.actionPerformed() displays, but when I press it again, it does not
dispose and the System.out statement does not display - the key click doesn't get to HotKeys.actionPerformed(). If I click the JButton, it does dispose and a click of the d key will
show it again.
Finally, I created two simple methods and replaced the JDialog stuff in HotKeys.actionPerformed() with calls to those methods; this worked fine so I know that my key binding works.
Any ideas?
I have included my Driver class, JPanel and JDialog code; note this is all bare-bones right now until I get the toggling working. I removed the package statement; put the files in a package of your choice.




 
Volodymyr Lysenko
Ranch Hand
Posts: 512
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

I did not go much into detail of your problem.
But I had also problem that my key bindings did not work.
This happens because JDialog or JFrame does not have focus.
They should programmatically reguest focus by requestFocus method and then key bindings will work.

This is how I remember it...
 
Rosie Fairfield
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Volodymyr! That did the trick.

However I did discover a problem: if I let the application and the JDialog appear in the default position, the JDialog is in the upper left corner. covering the application window; this is fine except that calling requestFocus() on the application (the JPanel) causes the dialog to go behind the JPanel. It needs to be in front of the JPanel. Is there a way to do this other than positioning the JDialog off of the JPanel?
 
Darryl Burke
Bartender
Posts: 5153
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Specify the existing JFrame as the owner.

And since this is about Swing, I shall move this thread to the section where we usually discuss such matters.
 
Rosie Fairfield
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Darryl! That works perfectly
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!