Solved

Stopping components from consuming F2 keystroke

Posted on 2002-04-15
7
1,821 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now