1 Reply Latest reply: Dec 7, 2012 9:28 AM by 840744 RSS

    How to override traversal keys CTL-TAB and CTL-SHIFT-TAB in JSplitpane?

    840744
      Recently added a JSplitpane to a frame with a number of other panels. One of those panels has two actions triggered by CTL-TAB and CTL-SHIFT-TAB. Since adding the JSplitpane those actions no longer trigger, apparently because JSplitPane is ManagingFocusForwardTraversalKeys and ManagingFocustBackwardTraversalKeys.

      I found if I disable the splitpane that the original actions are triggered, but that disables the divider.

      What would be the recommended way to change the behavior of a JSplitpane to ignore CTL-TAB and CTL-SHIFT-TAB?

      This is a list of things I have tried:
      bodySplitPane.getInputMap().remove(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.CTRL_MASK));
      bodySplitPane.getInputMap().remove(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.CTRL_MASK | InputEvent.SHIFT_MASK));
      bodySplitPane.getActionMap().remove("focusOutForward");
      bodySplitPane.getActionMap().remove("focusOutBackward");
      bodySplitPane.setFocusTraversalKeysEnabled(false); // doesn't work
      bodySplitPane.setFocusTraversalKeys(0,new HashSet<KeyStroke>());
      bodySplitPane.setFocusTraversalKeys(1,new HashSet<KeyStroke>());
      bodySplitPane.enableInputMethods(false);
      //bodySplitPane.setEnabled(false); // false allows ctl-tab to pass through but freezes the divider so it can't be moved via mouse
      Thanks in advance.
      Jeff

      Edited by: 837741 on Dec 6, 2012 1:11 PM

      Edited by: 837741 on Dec 6, 2012 1:13 PM
        • 1. Re: How to override traversal keys CTL-TAB and CTL-SHIFT-TAB in JSplitpane?
          840744
          Found this in the tutorial (http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) which solved the problem for me:
          bodySplitPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.CTRL_MASK),"none");
          bodySplitPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.CTRL_MASK | InputEvent.SHIFT_MASK),"none");
          Turns out my left splitpane has no focusable components, but might be that if it did I would have needed to set the action in the other two input maps as well like this:
          bodySplitPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.CTRL_MASK),"none");
          bodySplitPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.CTRL_MASK | InputEvent.SHIFT_MASK),"none");
          bodySplitPane.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.CTRL_MASK),"none");
          bodySplitPane.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.CTRL_MASK | InputEvent.SHIFT_MASK),"none");
          Contrary to what the tutorial says, "none" seems to work as the action for any of the input maps, and "nil" or any string seems to work. I found no need to define an action class as the tutorial states and the tutorial does not show any use of the action inner class.

          Also found this helpful http://www.ibm.com/developerworks/java/jdk/additional/IBM50KeyBindings.html

          Edited by: 837741 on Dec 7, 2012 7:25 AM