Java Swing How to - Disable the default key behaviour over a JTree








Question

We would like to know how to disable the default key behaviour over a JTree.

Answer

import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JTree;
import javax.swing.KeyStroke;
//from ww  w. ja v  a2  s.  c  o m
public class Main {
  public static void main(String[] args) {
    JTree tree = new JTree();
    InputMap inputMap = tree
        .getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    KeyStroke[] keyStrokes = inputMap.allKeys();
    for (KeyStroke keyStroke : keyStrokes) {
      Object actionCommand = inputMap.get(keyStroke);
      System.out.println("keyStroke = " + keyStroke);
      System.out.println("actionCommand = " + actionCommand);
    }
  }
}