Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / February 2007

Tip: Looking for answers? Try searching our database.

JDialog not consuming ESC key

Thread view: 
John T. Dow - 31 Jan 2007 06:30 GMT
I have a JDialog that responds to an ESC key regardless of which component
has the focus. This is what I want.

However, it seems that the esc key is not consumed, because upon disposing
of this dialog, when I open another, it immediately acts as if ESC has been
pressed.

I assume that the ESC key is not being consumed. Here's what I'm doing.
Blank lines inserted for clarity. Here I show using WHEN_FOCUSED,
WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, and WHEN_IN_FOCUSED_WINDOW all at once,
but I've of course tried them individually. I think
WHEN_ANCESTOR_OF_FOCUSED_COMPONENT is the appropriate one to use, but that
didn't seem to be firing. It's late at night and I just did all three for
the heck of it. One of them is working and the EscKeyPressed method is
being called. That much works.

KeyStroke esc = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);

DialogMainPanel.getInputMap(JComponent.WHEN_FOCUSED).put(esc, "ESCAPE");

DialogMainPanel.getInputMap
(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(esc, "ESCAPE");

DialogMainPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(esc,
"ESCAPE");

DialogMainPanel.getActionMap().put("ESCAPE", new EscKeyPressed());

John
Edwin Dankert - 31 Jan 2007 11:48 GMT
I guess you probably need to do this on the Root Pane instead, I
normally use:

getRootPane().getActionMap().put("escapeAction", new AbstractAction()
{
 public void actionPerformed(ActionEvent event) {
   setVisible(false);
 }
});

getRootPane()
 .getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
   .put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false),
"escapeAction");

Regards,
Edwin
--
http://www.edankert.com/
John T. Dow - 31 Jan 2007 12:56 GMT
"Edwin Dankert" <edankert@gmail.com> wrote in news:1170244132.911722.139120
@v33g2000cwv.googlegroups.com:

> I guess you probably need to do this on the Root Pane instead, I
> normally use:
>
> getRootPane().getActionMap().put("escapeAction", new AbstractAction()

That sounded like a good suggestion, but it didn't help. It's still not
consuming the esc key.

Also for some strange reason I have to use WHEN_FOCUSED for it to see the
esc key. (Maybe I have to experiment more which component has focus when I
press esc.)
Edwin Dankert - 31 Jan 2007 13:14 GMT
I realised after sending the email that I hadn't totally answered your
question ... sorry about that.

Your problem seems really strange but I think this has more to do with
the order in which you do things ... is your dialog modal? If that is
the case, I would have a look at when you make your dialog invisible
and open your second dialog?

Hope that helped?

Regards,
Edwin
--
http://www.edankert.com/
John T. Dow - 31 Jan 2007 19:41 GMT
Both dialogs are modal. This can't be the explanation as things work when I
click a button. It's just when I press the Esc key that the second dialog
closes prematurely.

"Edwin Dankert" <edankert@gmail.com> wrote in news:1170249277.183486.285460
@j27g2000cwj.googlegroups.com:

> I realised after sending the email that I hadn't totally answered your
> question ... sorry about that.
[quoted text clipped - 10 lines]
> --
> http://www.edankert.com/
Andrew Thompson - 31 Jan 2007 13:07 GMT
> I have a JDialog that responds to an ESC key regardless of which component
> has the focus. This is what I want.
>
> However, it seems that the esc key is not consumed, ...

Can you produce an SSCCE* that demonstrates
the behaviour?

* <http://www.physci.org/codes/sscce/>

Andrew T.
John T. Dow - 31 Jan 2007 19:43 GMT
No, I created a simple example and it works properly. Unfortunately the
real life program is far from simple. I'll have to see if I can selectively
simplify it to see what causes it to break. If anybody has suggestions,
please don't be bashful! This may take some time to figure out as it will
now have to be backburnered for a while.

>> I have a JDialog that responds to an ESC key regardless of which
>> component has the focus. This is what I want.
[quoted text clipped - 7 lines]
>
> Andrew T.
Andrew Thompson - 01 Feb 2007 04:23 GMT
> No,

'No' what?  Pleasre refrain from top-posting.

>...I created a simple example and it works properly. Unfortunately the
> real life program is far from simple.

Cool.  So 'all' you need to do is change the
complex program, one change at a time, to
being the same as the example, and one of
those changes will make the program work
for you.

>..I'll have to see if I can selectively
> simplify it to see what causes it to break. If anybody has suggestions,
> please don't be bashful!

Proceed with that strategy.  The SSCCE is
partly to come up with a working demonstration
of the problem, if the example fails to
demonstrate the problem, it is probably
because it contains (in some form) the solution.

>...This may take some time to figure out as it will
> now have to be backburnered for a while.

'All in good time'.

Andrew T.
Brandon McCombs - 02 Feb 2007 15:09 GMT
>> No,
>
> 'No' what?  Pleasre refrain from top-posting.

No, to the only question that was in the last post he responded to which
was your question.
chipu - 01 Feb 2007 16:05 GMT
have you tried the low level aproach instead?

addAWTEventListener(...);

and in the Listener:

awtEvent.consume();

Usually is not a good idea to use this low level method, but if
nothing else works...

PD: dont forget removing the listener when you JDialog is closed
John T. Dow - 01 Feb 2007 16:56 GMT
"chipu" <thrawnkb@gmail.com> wrote in news:1170345940.441366.138160
@l53g2000cwa.googlegroups.com:

> have you tried the low level aproach instead?
>
[quoted text clipped - 8 lines]
>
> PD: dont forget removing the listener when you JDialog is closed

Isn't the problem with a listener that you need one for each component that
might have focus.

Anyway, I think I figured out what the problem is. I just haven't had time
to investigate it yet.

My JDialog is built on the fly. It's a special purpose, throwaway copy of a
frame. That's why there's a difference between it and the simple example I
created, which of course works fine.

When I get it working, I'll post something.

Thanks

John
chipu - 02 Feb 2007 17:42 GMT
you add that listener to the toolkit:
getToolkit().addAWTEventListener(yourListener,
AWTEvent.KEY_EVENT_MASK);

is lower level that usual listeners. With that code you send *all* the
key events to the listener, no matter which component has the focus.
If the dialog is modal,
then this will work:

public void eventDispatched(AWTEvent event) {
            KeyEvent e = (KeyEvent) event;
            if (e.getID() != KeyEvent.KEY_PRESSED)
                return;
            if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
                                //close window
                                e.consume();
            }
        }
John T. Dow - 03 Feb 2007 09:01 GMT
"chipu" <thrawnkb@gmail.com> wrote in news:1170438123.647522.12460
@s48g2000cws.googlegroups.com:

> you add that listener to the toolkit:
> getToolkit().addAWTEventListener(yourListener,
[quoted text clipped - 14 lines]
>                }
>           }

getToolkit().getAWTEventListener works. Thank you.

I tried to simplify my code to get the inputmap and actionmap to work,
but just couldn't. I also double checked to make sure the problem was
that the esc key wasn't consumed (eg I modified the next dialog to not
respond to the esc key, and then it appeared and stayed instead of
appearing and instantly disappearing).

I wonder how the input and action maps are implemented. Perhaps they are
built on toolkit?

Anyway, case closed. I have a solution. Thanks everyone.

John


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.