Solved

Stopping components from consuming F2 keystroke

Posted on 2002-04-15
7
1,867 Views
Last Modified: 2008-11-27
I have a relatively complex UI including a SplitPane, several Tables, menu etc.

The Problem I have is that a menu item has F2 as a shortcut. This shortcut is set by my company, so I can't change it. Whenever I am editing data in a table and press the F2 key, that key is consumed somewhere along the way. I found that the JTable class has its own action for F2 but somehow I can't remove the action. I tried the following:

myTable.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0), "SaveAction");
myTable.getActionMap().put("SaveAction", new AbstractAction() {
        public void actionPerformed(ActionEvent e) {}
      });

and I've tried:

myTable.getInputMap().remove(KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0));

The first results in stopping the table from going into edit mode but my menu action is not called and the second still goes into edit mode.

How can I make sure that the F2 key really calls the menu action it is supposed to?

0
Comment
Question by:Calron
  • 3
  • 2
  • 2
7 Comments
 
LVL 9

Accepted Solution

by:
Ovi earned 200 total points
ID: 6943929
1. If you are not using a derived class of JTable you should start to.
2. In your derived class (SomeTable extends JTable), you should overwrite the protected method processKeyEvent(KeyEvent ke) inherited from the parent JTable. In this method you should filter which keys you are alow the table to consume and which not. Why to overwrite ? this method get's called before your listeners, and so the F2 key will be consumed. I will post a example shortly, but basically that's all the stuff you need. Also you can filter for all three types of key event received (pressed, released, typed).
By filtering meens that you threat the wanted keys for yourself in there and for those you are not interested in call for super.processKeyEvent(ke);
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6943943
 /**
   * Process incoming KeyEvents
   *
   */
  protected void processKeyEvent(KeyEvent e) {
    if (e.getID() == KeyEvent.KEY_PRESSED) {
      switch(e.getKeyCode()) {
      //disallowing default behaviour of the table for Enter, F3, F5, F8, and let the listeners to capture and use the keys.
        case KeyEvent.VK_ENTER :
          return;
        case KeyEvent.VK_F3:
          break;
        case KeyEvent.VK_F5:
          break;
        case KeyEvent.VK_F7:
          break;
        case KeyEvent.VK_F8:
          break;
        default :
          super.processKeyEvent(e);
          break;
      }
    } else {
      super.processKeyEvent(e);
    }
  }


... and some listeners which show you how the swith is done. Please ignore specific code, is taken directly from one of my projects.
// Inner key adapter class     ***********************************
  class HostKeyAdapter extends KeyAdapter {
    public HostKeyAdapter() {
    }

    public void keyPressed(KeyEvent e) {
      int code = e.getKeyCode();
      switch(code) {
        case KeyEvent.VK_C:
          if(e.isControlDown())
            cmd.enableDialog(true);
          if(e.isAltDown())
            enableCacheViewer();
          break;
        case KeyEvent.VK_R:
          if(e.isControlDown())
            bash.updateLS();
          break;
        case KeyEvent.VK_BACK_SPACE:
          if(e.isControlDown())
            hotlist.enableHotList(true);
          if(e.isAltDown())
            hotlist.addHotEntry(curentPath);
          break;
        case KeyEvent.VK_F1:
        case KeyEvent.VK_F2:
          if(e.isAltDown())
//                              EventManager.fireShortcutEvent(e, identity);
          break;
      }
    }

    public void keyReleased(KeyEvent e) {
      int code = e.getKeyCode();
      switch(code) {
//                    case KeyEvent.VK_ESCAPE:
//                              EventManager.fireShortcutEvent(e);
//                         break;
        case KeyEvent.VK_TAB:
          JCommander.observer.swapFocus();
          break;
        case KeyEvent.VK_F3:
          bash.view();
          break;
        case KeyEvent.VK_F5: // we made a copy
        // sinchronize the hidden with the real one
          synchronizeHiddenPath();
          bash.copy();
          break;
        case KeyEvent.VK_F7:
          bash.mkdir();
          break;
        case KeyEvent.VK_DELETE:
          bash.remove();
          break;
        case KeyEvent.VK_F8:
          bash.remove();
          break;
        default :
          curentRow = getSelectedRow();
          updateFilePath();
          if(code == 10) {
            unselectRow();
            updatePath();
            bash.ls(curentFile);
          }
          break;
      }
    }
  }
0
 
LVL 92

Expert Comment

by:objects
ID: 6944054
You need to unregister the keyboard action:

myTable.unregisterKeyboardAction(KeyStroke.getKeyStroke(KeyEvent.VK_F2,0));

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Author Comment

by:Calron
ID: 6959108
Thanks Ovi, for that answer. It helped me find the correct answer.

Actually the way that I solved it in the end is the following:

table.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0), "ProjSave");
    table.getActionMap().put("ProjSave", new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
          mParentPanel.handleMessage(ProjConst.ID_FILE_SAVE);
        }
      });

Like that I don't have to subclass JTable and still do pretty much the same thing that your code does.
0
 
LVL 92

Expert Comment

by:objects
ID: 6959125
did u try unregistering the action, it works for me?
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6959133
Thanks
0
 
LVL 5

Author Comment

by:Calron
ID: 6959207
I tried unregistering the action. In my case using JTable it does not work. The F2 action on JTable is still called. In addition to that according to the API docs that method is now obsolete. Here a copy of the API docs:

unregisterKeyboardAction(KeyStroke aKeyStroke) This method is now obsolete. To unregister an existing binding you can either remove the binding from the ActionMap/InputMap, or place a dummy binding the InputMap. Removing the binding from the InputMap allows bindings in parent InputMaps to be active, whereas putting a dummy binding in the InputMap effectively disables the binding from ever happening. Unregisters a keyboard action. This will remove the binding from the ActionMap (if it exists) as well as the InputMaps.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to determine if a string is a valid SHA value 7 43
eclipse console opening separately 2 35
Eclipse Java import and method not resolved 4 73
Coding for the first time 9 62
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question