Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Stopping components from consuming F2 keystroke

Posted on 2002-04-15
7
Medium Priority
?
1,922 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 800 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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
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 to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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 will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

721 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