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
Solved

Stopping components from consuming F2 keystroke

Posted on 2002-04-15
7
1,860 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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
servlet example 11 49
Configure a Bean in an XML file 4 42
Java exception bubble up 2 21
Tagging and Merging on Branch 1 29
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

856 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