Results 1 to 6 of 6
  1. #1
    sh4dyPT's Avatar
    sh4dyPT is offline Member
    Join Date
    Mar 2009
    Location
    German
    Posts
    17
    Rep Power
    0

    Angry Cannot Listen TAB key

    Hello
    I have JTextField which has KeyListener associated.
    I can't understand why the TAB key is not captured with this KeyListener.

    Here is my code.
    Java Code:
             public void keyPressed(KeyEvent key) {
    		if(key.getKeyCode() == KeyEvent.VK_TAB)
    			System.out.println("TAB!!!!!!!!!");
    	}
    	
    	public void keyTyped(KeyEvent key) {
    		if(key.getKeyCode() == KeyEvent.VK_TAB)
    			System.out.println("TAB!!!!!!!!!");
    	}
    	public void keyReleased(KeyEvent key) { 
    		if(key.getKeyCode() == KeyEvent.VK_TAB)
    			System.out.println("TAB!!!!!!!!!");
    	}
    I have already tried change to
    <MyJTextField Component>.setFocusTraversalKeysEnabled(true);
    <MyJTextField Component>.setFocusTraversalKeysEnabled(false);
    without success, and the strange think is when I press CTRL+TAB, the TAB Key is capured.


    Thank you!
    Any help will be appreciated.
    Last edited by sh4dyPT; 07-15-2009 at 09:07 PM.
    Ubuntu, Linux for human Beings!

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    The forum software will not accept the code I want to post to demonstrate what is happening with respect to the tab key. A key binding for the tab key is being used as a focusTraversalKey by the textField or an ancestor container.
    I'll try for some selected snippets:
    Java Code:
            // intercept all keyboard input
            KeyboardFocusManager kfm =
                KeyboardFocusManager.getCurrentKeyboardFocusManager();
            kfm.addKeyEventDispatcher(new KeyEventDispatcher() {
                public boolean dispatchKeyEvent(KeyEvent e) {
                    if(e.getKeyCode() == KeyEvent.VK_TAB)
                        System.out.println("TAB!!!!!!!!!");
                    return false;
                }
            });
    Java Code:
        private void exploreTraversalKeys(JComponent jc) {
            Set keys = jc.getFocusTraversalKeys(
                KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
            Iterator it = keys.iterator();
            while(it.hasNext()) {
                System.out.println(it.next());
            }
        }
    Java Code:
            JTextField textField = new JTextField(12);
            JPanel panel = new JPanel();
            panel.add(textField);
            // Look for a key binding of the tab key that might be
            // consuming the KeyEvent generated by the tab key
            // being pressed when the cursor is in textArea.
            // this method won't pass forum software filters
    //        exploreKeyBindings(textField);
            exploreTraversalKeys(textField); // found
            return panel;
        }

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by hardwired View Post
    The forum software will not accept the code I want to post to demonstrate what is happening with respect to the tab key.
    I'm not sure why there's a glitch here, but one way around it is to create a JAR with source files, change the extension to .zip, and post the zip file. Best of luck!

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    create a JAR with source files, change the extension to .zip, and post the zip file
    I'll try it. Thanks.
    Attached Files Attached Files

  5. #5
    sh4dyPT's Avatar
    sh4dyPT is offline Member
    Join Date
    Mar 2009
    Location
    German
    Posts
    17
    Rep Power
    0

    Default

    This is exactly what I was looking for. Thank you very much
    Ubuntu, Linux for human Beings!

  6. #6
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    I tried to rep Fubarable in link 3 but it doesn't seem to have been recorded. The message was "Thanks for the help."

Similar Threads

  1. Replies: 3
    Last Post: 05-22-2011, 03:16 PM
  2. how to listen for buttons between jpanels?
    By pjr5043 in forum New To Java
    Replies: 17
    Last Post: 09-29-2008, 03:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •