JComboBox consumes Escape Key

Hi,
I have a JDialog on which I am putting various components. I have used to the following code so that the cancel() method is called when the esacpe key is pressed.
String CANCEL_ACTION_KEY = "CANCEL_ACTION_KEY";
AbstractAction cancelAction = new AbstractAction(){
public void actionPerformed(ActionEvent e){
cancel();
this.getRootPane().getActionMap().put(CANCEL_ACTION_KEY, cancelAction);
KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
InputMap dialogInputMap = this.getRootPane().getInputMap
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
dialogInputMap.put(escapeKeyStroke, CANCEL_ACTION_KEY);
This works for all the components except for the JComboBox. Is there any way I can get the JComboBox not to consume the key event if it is not open? (i.e. of the combo box is open, i want to be able to cancel the second time escape is pressed.)
The JComboBox is in a panel on another panel inside the JDialog. It's a type of wizard, and the componets are loaded dynamically by reflection, so the JComboBox doesn't have a reference to the action (or the cancel method).
Maybe there's a way I could override one of the JComboBox's methods so it doesn't consume the key event if it is closed? (hence sending it up to the root pane?)
Any help would be greatly appreciated!
Thanks,
Rob

I've got it!
Cheers for the reply vinny.... but the answer just came to me this morning. It's so simple! I override the processEvent method of the ComboBox like so: (super is the ComboBox)
public void processKeyEvent(KeyEvent e)
if((e.getKeyCode() == KeyEvent.VK_ESCAPE) && !super.isPopupVisible()){
return; //don't process the event
super.processKeyEvent(e);
So, if the list is visible, escape will process the event and close the list .
If escape is pressed again, list is not visible so the ComboBox will not process the event, sending it up to it's parent. Perfect operation.
In my JDialog I have the following code to catch the escape key:
private void addCancelByEscape()
AbstractAction closeDialogAction = new AbstractAction(){
public void actionPerformed(ActionEvent e){
cancel();
String actionName = "close-dialog";
KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
JLayeredPane layeredPane = this.getLayeredPane();
layeredPane.getActionMap().put(actionName, closeDialogAction);
layeredPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(stroke, actionName);
This seems to work a treat. I also have a JTree on the dialog, and this seems to consume the escape key aswell. I've overridden the processKeyEvent for that (similar to above) and it works aswell! Thanks for the help.
I might be including editable JComboBoxes soon.... if I come across any info or help i'll let you know....

Similar Messages

  • My Escape key is not working

    Hi,
    I am Meena.
    I am doing a project using NetBeans and MS-Access.
    When I run my project a internal framed named login loaded on the jdesktoppane which in JFrame.
    So in the login internal frame i used two buttons one for Sign-in and another for sign out.
    So I combined the Sign-in button with Enter key and Sign-out button with Escape key by the following coding.
    I wrote the following coding in LoginInternalFrame->properties->enabled->advanced->Generate PreIntialization Code.
    Action EscFromLogin=new AbstractAction()
    public void actionPerformed(ActionEvent e)
        jButton2.doClick();
    Action OkFromLogin=new AbstractAction()
        public void actionPerformed(ActionEvent e)
           jButton1.doClick();
    getRootPane().getInputMap(jButton2.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE,0,false),"Esc_in_Login");
    getRootPane().getActionMap().put("Esc_in_Login",EscFromLogin);
    getRootPane().getInputMap(jButton1.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0,false),"Ok_in_Login");
    getRootPane().getActionMap().put("Ok_in_Login",OkFromLogin);In the above coding Enter key works well no problem.that is when I press the Enter key ,it combined with Sign-in button and works well.
    But when I press Escape key,it doesnot combined with Sign-off button.
    that is nothing is happening when I press Esc key.
    So i change the Escape key and test with F1 then F2 then F3 all are combined with Sign-off button and works well.But only the escape key is not working.
    But I have so many internal frames .In all internalframes my esc key works well.But not in the first inter frame(login internalframe)
    I couldnot understand the reason.
    Will you please help me to solve.
    Thank you so much.
    Meena

    mmm, i'm doing some tests and the case is working for me... The reason F1,F2 keys works while ESC don't is that some component is "eating" the ESC key. Probably your password textField, but it is not its default behaviour. For example, try to replace the textField with a FormattedTextField, write something into it and press ESC, this first ESC is consumed by the FormattedTextField, if you press ESC a second time the FormattedTextField not consume it, so the ESC keystroke processing is given to the rest of the componentes until one of they consume it (basically). So, how can you solve th problem? Try to determine which component is the "escape-eater". Once you detected it try to determine which properties are set and if this properties can alter the keyboard mapping. The problem can be difficult to repeat for us because the look and feel, java version, etc. can affect the system behaviour too. A global hook to ESC can also be the reason (maybe esc key it's not reaching ths components), try to override ProcessKeyBindings and log the results....

  • Escape Key Event in JTable

    I have a KeyPressed event method in a java file where I am using the F3 and Escape keys. I have a JTable in the java file with values in it. The user can select any row in the table and when you press F3 or Escape key some methods get called up which deselect the chosen rows in the table. This works fine with the F3 key but when I put the exact same code in the Escape key event, nothing happens.
    However when the focus goes off the JTable to another componement on the screen, pressing the Escape key now works. I want to have the Escape key working when the focus is on the table. Any suggestion please?

    thanks for you response but I am still a bit confused. When I press the Esc key whilst the focus is on the JTable nothing happens but when the focus is off the JTable, all the methods in the Esc event get called and the rows in the table get deselected.
    Is there any way of not having the JTable consuming the Esc key so the Esc key will work when focus is on the JTable.
    I have a key pressed method registered to the class (not the JTable). Say that my class is called 'xinternetDesktop', my code is this:
    public void xinternetDesktop_KeyPressed(java.awt.event.KeyEvent keyEvent) {
    int code = keyEvent.getKeyCode();
    switch (code) {
    case KeyEvent.VK_ESCAPE : /*Escape key*/
    resetListSelectionBidsAndOffers();
    break;
    case KeyEvent.VK_F3 : /*F3*/
    resetListSelectionBidsAndOffers();
    break;
    Could u give any suggestion please.
    Thank you

  • Default behaviour of the Escape key while editing a cell in JTable??

    Hi all,
    i have a Jtable which get its data from an own model object which extends DefaultTableModel.
    If i overwrite the isCellEditable(row, col) method within the tablemodel object and set a specific column to editable, then i notice while editing a cell in that column that the default behaviour of the Escape key is that independet from what you have entered before in to the cell the editing stops and the cell gets the value it had before editing.
    This is the case for me even if i apply a custom editor to a column (one that extends DefaultCellEditor). It is just a JTextField that limits the number of digits a user can enter. Everything works fine. If the user edits the cell and presses ENTER or the "down arrow key" i can check what he has entered with the help of the getCellEditorValue() method. But if the user hits the ESC key after editing a cell this method is not invoked!!!
    My question is :
    is there any way to detect that the user cancels editing with the ESC-key.
    this is very important for me because if the user goes editing the cell i lock the related record in the database, if i cannot detect this it is locked till the application terminates.
    Thanks for any help in advance

    I try override the JTable editingCanceled() ==> does not work.
    I try the addCellEditorListener( CellEditorListener l ) ==> does not work.
    Finally, I try the addKeyListener ==> it works.
    Here is a quick demo. program:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.event.*;
    import javax.swing.table.*;
    public class Test {
    public static void main(String[] args){
    JFrame f = new JFrame();
    String[] colName = {"a", "b"};
    String[][] rowData = {{"1", "2"}, {"3", "4"}};
    JTable table = new JTable(rowData, colName);
    JTextField t = new JTextField(10);
    t.setBackground(Color.red);
    t.addKeyListener(new KeyAdapter() {
    public void keyPressed(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
    // do what ever you want ex. un-lock table
    System.out.println("ESCAPE");
    DefaultCellEditor editor = new DefaultCellEditor(t);
    TableColumnModel colModel = table.getColumnModel();
    for (int i = colModel.getColumnCount()-1; i >= 0; i--) {
    colModel.getColumn(i).setCellEditor(editor);
    f.setContentPane(new JScrollPane(table));
    f.pack();
    f.setVisible(true);

  • When I hit the escape key to close out the open file dialog box, firefox hangs or crashes.

    When I open the "open file" dialog box using the ctrl+O and then hit the escape key to exit the box, I get an error message which I'm not able to paste here.
    it says something about a runtime problem and if I try the same thing again to reproduce the problem, it will sometimes just hang and windows vista will tell me the program is not responding.
    I've checked to see if there are any crash IDs but none were found for this time frame.
    I have even disabled all add ons.
    I'm currently using vista 64 bit edition and Firefox 6, beta edition.

    Hi,
    Please also try right-clicking on the link corresponding to the language version and '''Save Target As''' to save the Firefox installer. If the problem persists, posting [http://answers.microsoft.com/ here] would also be helpful.
    [https://www.mozilla.org/en-US/firefox/all.html Firefox Latest]

  • How to disable detection of ESCAPE key in JDialog?

    Hi,
    I've implemented a non-modal JDialog, and to be sure the user doesn't close it I call setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE)However, the user can always close it by pressing the ESCAPE key.
    Key binding does not work as the dialog is not a JComponent, and a KeyListener seems not to work.
    Is there a way to catch the ESCAPE key?
    Thanks in advance for any hint.

    However, the user can always close it by pressing the ESCAPE key.Is this a new feature in JDK1.5 or JDK6?
    The escape key is not supported by default in JDK1.4.2 so I'm surprised that its turned on by default in later versions.
    This is how I add the funtionality in JDK1.4.2:
    Action escapeAction = new AbstractAction()
         public void actionPerformed(ActionEvent e)
              System.out.println("escape");
              setVisible( false );
    KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
    getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
         .put(escapeKeyStroke, "escape");
    getRootPane().getActionMap().put("escape", escapeAction);So, presumably you could override the default behaviour by using a dummy Action.

  • Using the Escape key in simulations and assessments

    I have recently recorded a simulation/assessment that needs to use the Escape key to close a pop-up window and returns to the main application window. The recording has not been an issue; however, using Escape with failure messages is proving to be tricky.  Here's what I have discovered while resording my sim/assess, along with my questions:
    When I press Escape, Captivate captures this keystroke as a shortcut attached to a Click Box. This works on the assessment side, but fails to produce a failure message on the assessment side when the user does not press the Escape key.
    I need to produce a failure message when the user  does not press Escape.
    Can I storethe unicode value of Escape in a TEB variable and then use an if . . . else statement to either advance to the next slide (success action) or produce a caption that mimics a failure caption?  If so, how would I write this item?
    Could I use a Loss of focus trigger instead?
    Does Captivate have a variable that records a unicode value (Something like CPKeyPressedValue"?
    Are there any other alternatives?

    In the case of buttons and clickbox objects, where clicking is allowed, the failure is normally registered when the user CLICKS OUTSIDE the button or click box.  The evaluation of Success/Fail happens on the click.
    In the case of a Text Entry Box, the interactivity is all about what the user enters into the text field and whether or not it is correct/incorrect when compared to approved answer text.  The main purpose of the TEB object is not centered on the clicking of the button that accompanies the text field.  The button just provides a way to trigger the Success/Failure evaluation of the user interaction with the text field.
    Since you have deselected Allow Mouse Click for your click box, you've effectively made it mandatory to use keystrokes to trigger the Success/Failure evaluation of the object. So clicking outside the box is no longer going to do anything because Allow Mouse Click is off.  You'll get a failure message on this click box now for many (but not all) keystrokes that are not the one assigned.
    By the way, using the Escape key as a keystroke if you Preview the project in Captivate just shuts down the preview (as you've probably already found out).

  • Escape Key Not Working

    Hi Gang
    Was surprised to see the escape key can no longer end/stop a capture. I have to manually stop the camera and wait a second for FC to say 'reached end of edit/tape'. I poked around in system Prefs and FC Keyboard Functions - couldn't find anything?
    Umm?
    Mike

    Hi James
    Actually I spoke too soon ....
    The problem returned.
    I cleared the keyboard commands - reset them - escape performed ok the 1st time, but then the problem returned again

  • How can I get the escape key to work again?

    I've used "esc" to get out of VI's insert mode for 40 years. I am not going to relearn that keystroke as "ctl-esc", period. Even if I were to do that, then I'd be typing ctl-esc on other platforms where it would not work because I have to type "esc" alone. If there is a way to fix in in System Preferences->keyboard, I cannot find it. If there is not, then it is completely 100% brain-dead wrong. I hope that's not the case.  So there is definitely a bug:
    The key-up event on the escape key does not cause the escape key code to be delivered to the active window when no other key is pressed, or
    The UI is sufficiently obtuse that the cause of the suppression cannot readily be found.
    I do hope it is merely the latter because I am compelled to use Outlook and Outlook does not work properly under OS/X 10.6.  (Our Exchange Server is rigged to only allow Outlook clients to send email outside of the company.)
    Thank you.

    The answer is:  The bug is in the UI.  Speech recognition was enabled, by default it uses the bare escape key as a "mic on" indicator, and it does not leave any trace in the keybord configuration.  So that is the bug.  It needs to leave a trace there, a la: "X Listen-for-voice-command  ^" under Mission Control.

  • Escape key no longer functioning to back out of photos in Event view

    In iPhoto '09, when you drilled down to view a photo in Event view, you used to be able to use the Escape key to step back a level. With iPhoto '11, I have been unable to do this. Is there a setting I'm missing or something?

    I just figured out a great solution to this problem! The goal here is to remap the Escape key in iPhoto so that it functions like the Command-Left-Arrow, which currently has all the functions we wish Escape had. The instruction are fairly simple:
    Install KeyRemap4MacBook. It's a wonderful program that lets you take control of your keyboard. It's stable and powerful and quick and free.
    Follow these instructions which tell you how to add your own custom settings. (Skip steps 4 through 6, which just give you some example settings to add.)
    Edit private.xml so that it looks like the following (or at least contains the following "item", if you have other custom settings installed):
    <?xml version="1.0"?>
    <root>
      <item>
        <name>Escape to Command Left Arrow</name>
        <identifier>private.app_iphoto_escape_to_command_left_arrow</identifier>
        <only>IPHOTO</only>
        <autogen>--KeyToKey-- KeyCode::ESCAPE, KeyCode::CURSOR_LEFT, ModifierFlag::COMMAND_R</autogen>
      </item>
    </root>
    Finally, save private.xml, then click "ReloadXML" under the "Change Key" tab of the KeyRemap4MacBook preferance pane. Your new setting should appear at the top of the list. Enable it. Restart iPhoto if you feel like it (you probably won't need to). And now, in iPhoto, Escape should work as you want it to!
    NOTE: There is one downside I've discovered. You won't be able to get out of Slideshow mode using the Escape key anymore, as this was the one case where Escape originally functioned sensibly. Rather, you'll have to use your mouse to click the X button to escape. But Slideshow mode is terrible anyway. Just use full screen mode instead.

  • Mod4 as escape key in screen?

    I was wondering whether and how the C-a combination in screen could be replaced by the "windows" key

    I have two 8900 curves on my desk.
    On one I do notice the escape key seems to have a "less deeper" push (doesn't travel as far as the Menu key).
    On the other, they feel to be about the same.
    So... I would say that is fairly normal.
    1. If any post helps you please click the below the post(s) that helped you.
    2. Please resolve your thread by marking the post "Solution?" which solved it for you!
    3. Install free BlackBerry Protect today for backups of contacts and data.
    4. Guide to Unlocking your BlackBerry & Unlock Codes
    Join our BBM Channels (Beta)
    BlackBerry Support Forums Channel
    PIN: C0001B7B4   Display/Scan Bar Code
    Knowledge Base Updates
    PIN: C0005A9AA   Display/Scan Bar Code

  • Escape key in jtable while editing

    I am trying to unregister the escape key from the jtable when it is in an editing mode. The field in an editable mode is a JTextField. I tried:
    this.getInputMap().remove(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0));I also tried removing it from the JTextField in MyCellEditorObject (which extends DefaultCellEditor)
    But the escape key does not seem to be in either of these components when I do:
    KeyStroke[] ks = this.getInputMap().allKeys();
    if(null == ks)
        System.out.println("null ks........................");
    else
        for (int i = 0; i < ks.length; i++)
            System.out.println(ks.toString());
    Once I get this figured out I need to add and remove other keys to whatever object is handling the events.
    Does anyone know what object is handling the escape key in the JTable while it is in an editing state using a JTextField?

    Well, for others who may have the same problem this is what I found so far:
    The jtable.getInputMap() will always return null (hardcoded in BasicTableUI). To get the input map one has to put:
    jtable.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
    Then
    KeyStroke[] ks = this.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).allKeys();
    returns keys in the input map and the parent input map....(.keys() will exclude the keys in the parent input map).
    For the JTable all the keys are registered in the parent inputMap.
    m_table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).getParent().remove(KeyStroke.getKeyStroke(KeyEvent.VK_ALPHANUMERIC, 0));
    m_tablegetInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).getParent().remove(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false));
    m_table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).getParent().put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, true), "quitRelease");
    m_table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).getParent().put(KeyStroke.getKeyStroke(KeyEvent.VK_F9, 0), "actionMApKeyObject");
    m_table.getActionMap().put("quitRelease", new QuitAction());
    m_table.getActionMap().put("send", new SendAction());
    private class QuitAction  extends AbstractAction
        public void actionPerformed(ActionEvent ae)
           //do quit cellediting stopped? yes then press quit btn no thenstop ...
    private class SendAction  extends AbstractAction
        public void actionPerformed(ActionEvent ae)
           //do send
    }I still would like to know more about the input maps...and the parent input map???
    As well as the ActionMaps all introduced in 1.3.
    Does anyone know of any URLs or books that contain good explanations and samples
    (I have not found any)...
    I am also interested in how this could be applied more globally
    like the enter key pressing all buttons in my application.
    Or having text fields respond to keyReleased rather than pressed.

  • Using escape key in finder to navigate backwards

    wondering why this function was removed. in mountain lion i could navigate backwards in a finder window by hitting the "escape" key. each press of the button would take me backwards through my finder path the same way that i came into whatever folder i was in. like a bread trail through the woods, it was really nice.
    any way to reactivate in mavericks? why would you take away something so simple and functional?

    Why always the best options are removed in the latest version, It happens with Adobe always, now Apple joined it seems.
    Pressing Escape let me out from selected folder in Finder window, it helps me move around top and bottom folder with help of arrow keys.
    Now, it's uneasy to come out of the folder without mouse.
    Whats a breakthrough here, any command which activate this function back?

  • Exiting programs using the escape key

    My team and I are design games for autistic children. One of the problems we are having is that we do not know how to quit the application using the escape key. Our team would really appreciate it if you could tell us how this is done.

    Quick example:
    import java.awt.event.*;
    import javax.swing.*;
    public class ExitOnEscape extends JFrame
        public ExitOnEscape()
            setSize(300, 300);
            addKeyListener(new KeyAdapter()
                public void keyPressed(KeyEvent e)
                    int key = e.getKeyCode();
                    if(key == KeyEvent.VK_ESCAPE)
                        System.exit(0);
            setVisible(true);
        public static void main(String[] args)
            new ExitOnEscape();
    }

  • Escape key not recognized

    greetings.
    i just recently switched over to a new computer at work and using another designer's workstation w/ cs3.  for some reason unknown to me, the "ESC" (escape) key will not work for me when trying to deactivate a text box.
    i looked under the keyboard shortcuts menu and found where to input this shortcut (keybord shortcuts/product area: tools/selection tool (highlighted) ...and see the current shortcuts set at "default: v" and "text: escape" so i know it's there.
    however, when i try to input a new shortcut and hit the escape key, it's not being recognized at all.  same thing happens when trying to set the escape key for any other shortcut.
    i'm experiencing the same issue in illustrator, as well.
    any thoughts are greatly appreciated.

    Ps CS4 11.0.2 (64-bit)
    Windows 7 Pro SP1
    Apologies for bumping this oldie. Escape key is completely dead in and outside of Ps.
    The weird thing is that it can only be reproduced by opening Ps (with or without a file) while CCC.exe is running (AMD VISION Engine Control Center).
    So one would be inclined to blame Ps.
    Adobe staff and users carrying a tarred and feathered ATI driver writer, out of town on a train rail :
    The problem returned with the following AMD/ATI driver :
    [QUOTE]Driver Packaging Version    8.872-110707b-122569C-ATI   
    Catalyst Version    11.7   
    Provider    ATI Technologies Inc.   
    2D Driver Version    8.01.01.1171   
    2D Driver File Path    /REGISTRY/MACHINE/SYSTEM/ControlSet001/Control/CLASS/{4D36E968-E325-11CE-BFC1-08002BE1031 8}/0000   
    Direct3D Version    7.14.10.0847   
    OpenGL Version    6.14.10.10907   
    AMD VISION Engine Control Center Version    2011.0707.2346.40825   
    [/QUOTE]
    Closing CCC.exe in the Windows task manager, then restarting Ps cured it.

Maybe you are looking for