Solved

Stopping components from consuming F2 keystroke

Posted on 2002-04-15
7
1,899 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.

636 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