I want the user to be able to input tabs in my application, but the tab character moves the input focus to the next component in the app and my application's keylistener gets nothing. Is there any way to change this behaviour ?

Nils Christian Svihus

The tab is a focus traversal key by default and you have to change this to catch the tab entries in your keylistener. You do this by overriding the Component method : getFocusTraversalKeys(int id).
Here is the basic code for doing this.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

/**
 * Listen to keys typed, including TAB
 */
public class KeyListenerFrame extends JFrame implements KeyListener {

  GridLayout gridLayout1 = new GridLayout();
  JPanel listenerPan = new JPanel();
  HashSet emptySet=new HashSet();

  /**
   * Constructor
   */
  public KeyListenerFrame() {
    try {
      jbInit();
      listenerPan.addKeyListener(this);
      addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
          System.exit(0);
        }
      });
      /**
       * As an example I hook the pageUp -key to an actionhandler
       */
      listenerPan.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP,0,true),
                             "showKey");
      listenerPan.getActionMap().put("showKey",
                              showKey);
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  /**
   * Call this function to ensure key-focus for the listenerPanel
   */
  public void focusPanel() {
     listenerPan.requestFocus();
  }
  /**
   * GUI setup
   */
  private void jbInit() throws Exception {
    this.getContentPane().setLayout(gridLayout1);
    this.getContentPane().add(listenerPan, null);
  }
  /**
   * Define an actionhandler
   */
  Action showKey = new AbstractAction() {
    public void actionPerformed(ActionEvent e) {
        System.out.println("VK_PAGE_UP");
    }
  };

  /**
   * KeyListener implementation
   */
  public void keyTyped(KeyEvent e) {
    System.out.println(e.toString());
  }
  public void keyPressed(KeyEvent e) {
   System.out.println(e.toString());
  }
  public void keyReleased(KeyEvent e) {
    System.out.println(e.toString());
  }
  /**
   * Ensure that the forward/backeard traversal key (the tab)
   * does not move the component focus, - return an empty key set.
   * @param id
   * @return
   */
  public Set getFocusTraversalKeys (int id) {
    if (id==KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS ||
        id==KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS) {
      return emptySet;
    } else return super.getFocusTraversalKeys(id);
  }
  /*************************************************
   * MAIN
   */
  public static void main(String[] args) {
    KeyListenerFrame klf = new KeyListenerFrame();
    klf.setBounds(10,10,200,200);
    klf.setVisible(true);
    klf.focusPanel();
  }
}
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact