Listening Tab Key Event On JTextField

Hi
i am unable to listen Tab Key Event on JTextField ....
tField.addKeyListener(new KeyAdapter()
public void keyPressed(KeyEvent e)
if (e.getKeyCode()==e.VK_TAB)
JOptionPane.showMessageDialog(null,"Tab Pressed");
else
JOptionPane.showMessageDialog(null,"Tab Not Pressed");
When i pressed Tab Key then it dont come in KeyPressed Event ...Actually it go towards next FocusAble component ..
Any help in this regard.....

I also Checked it by using
tField..setFocusTraversalKeysEnabled(false);
tField.addKeyListener(new KeyAdapter()
public void keyPressed(KeyEvent e)
if (e.getKeyCode()==e.VK_TAB)
JOptionPane.showMessageDialog(null,"Tab Pressed");
else
JOptionPane.showMessageDialog(null,"Tab Not Pressed");
In this case each time e.getKeyCode() returns 0...
plz help me

Similar Messages

  • Capturing the TAB key event.

    How can I capture the TAB key event?
    I am trying to use it within a JTextField to do a normal text tab operation.
    Thanks,
    D

    I want to map the Tab key to a jTextField so that when the user is editing inside the jTextField they can press the TAB key to tab over a predefined number of characters.
    Thanks,
    D

  • [JS] Tab key event (ScriptUI)

    Hi,
    If set to 'window' or 'palette' type, a ScriptUI Window can't receive 'Tab' key events on WinXP.
    Is this the same with other OS?
    Is there a workaround?
    Thanks,
    Marc

    Here's the thread about the bug
    http://forums.adobe.com/message/2068532
    It really was and is a big problem for me and soesn't seem to be getting fixed any time soon
    Steven Bryant
    http://scriptui.com

  • How to recognize the tab key in a JTextField

    I have a drawing program with a main window and a tools palette, which is a JDialog. The tools palette has JToggleButtons and one JTextField. When you have the focus in the JTextField and you press tab repeatedly, it tabs through all the JToggleButtons and then back into the JTextField.
    However I would like to recognize the pressing of the tab key and ask for the focus to go back to the main window.
    I subclassed the JTextField, added a KeyAdapter, but it does not recognize the tab key. I also added the processKeyBinding method, but it doesn't recognize the tab key either.
    How can I recognize the tab key?
    Here is a self-contained test program that illustrates the problem:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class AnanyaCurves extends JFrame
      Tools tools;
      public AnanyaCurves(Dimension windowSize)
        Basics.ananyaCurves = this;
        enableEvents(AWTEvent.WINDOW_EVENT_MASK);
        setTitle("Ananya Curves");
        tools = new Tools(this);
      public static void main(String[] args)
        int toolsWidth;
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        toolsWidth = 200;
        Dimension windowSize = new Dimension(screenSize.width - toolsWidth, screenSize.height - 58);
        AnanyaCurves ananyaCurves = new AnanyaCurves(windowSize);
        ananyaCurves.pack();
        ananyaCurves.setBounds(toolsWidth, 0, windowSize.width, windowSize.height);
        ananyaCurves.setVisible(true);
        ananyaCurves.requestFocus();
      public void setVisible(boolean b)
        tools.setVisible(b);
        super.setVisible(b);
    class Basics extends java.lang.Object
      public static AnanyaCurves ananyaCurves;
      public Basics()
    class Tools extends JDialog
      JToggleButton btnGrid;
      JTextField textGrid;
      JPanel panel;
      public Tools(JFrame frame)
        super(frame, "Tools", false);
        enableEvents(AWTEvent.WINDOW_EVENT_MASK);
        btnGrid = makeBtn("Grid");
        textGrid = makeTextField();
        panel = makePanel();
        pack();
      public JToggleButton makeBtn(String name)
        JToggleButton btn = new JToggleButton();
        btn.setMaximumSize(new Dimension(108, 24));
        btn.setPreferredSize(new Dimension(108, 24));
        btn.setText(name);
        btn.setFont(new Font("Verdana", Font.PLAIN, 11));
        btn.setMargin(new Insets(5, 10, 5, 10));
        btn.setOpaque(true);
        return btn;
      public ACJTextField makeTextField()
        ACJTextField textField = new ACJTextField();
        textField.setFont(new Font("Verdana", Font.PLAIN, 12));
        textField.setMaximumSize(new Dimension(108, 20));
        textField.setPreferredSize(new Dimension(108, 20));
        textField.setText("0.25");
        textField.setEnabled(true);
        return textField;
      public JPanel makePanel()
        JPanel panel = new JPanel();
        panel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.add(btnGrid);
        panel.add(textGrid);
        getContentPane().add(panel);
        return panel;
    class ACJTextField extends JTextField
      KeyAdaption keyAdaption;
      public ACJTextField()
        super();
        keyAdaption = new KeyAdaption();
        this.addKeyListener(keyAdaption);
      class KeyAdaption extends KeyAdapter
        public void keyPressed(KeyEvent event)
          int keyCode = event.getKeyCode();
          if (keyCode == KeyEvent.VK_TAB)
            Basics.ananyaCurves.requestFocus();
      protected boolean processKeyBinding(KeyStroke keyStroke, KeyEvent keyEvent, int int2, boolean boolean3)
        int keyCode = keyEvent.getKeyCode();
        if (keyCode == KeyEvent.VK_TAB)
          Basics.ananyaCurves.requestFocus();
          return false;
        return super.processKeyBinding(keyStroke, keyEvent, int2, boolean3);
    }Thanks for looking at this.

    Wow, Michael, you work like magic! Thanks so much! I would be happy to give you a commercial key to my Ananya Curves program also once the second release is on my website. It's a program for drawing curves in an easier way without pulling on tangent lines, with all control points right on the curve. Just send me an email to [email protected] if you are interested!

  • Tab key event not fired in JTabbedPane

    Hello,
    I wanted to add the functionality of moving between tabs when Ctrl+Tab is pressed like Windows tabs.
    But the tab key listener event is not getting fired by pressing Tab. For all the other key pushes it is working good. It this a problem with Java? Any workarounds?
    Here is the sample code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class TestTab extends JFrame
         public void drawGUI()
              //getContentPane().setLayout(new GridLayout(1, 1));
              JTabbedPane lTabbedPane = new JTabbedPane();
              JPanel lFirstPanel = new JPanel();
              lFirstPanel.add(new JLabel("First Panel"));;
              JPanel lSecondPanel = new JPanel();
              lSecondPanel.add(new JLabel("Second Panel"));;
              lTabbedPane.addTab("Tab1", null, lFirstPanel, "Does nothing");
              lTabbedPane.addTab("Tab1", null,lSecondPanel, "Does nothing");
              getContentPane().add(lTabbedPane);
              lTabbedPane.addKeyListener(new KeyAdapter()
                   public void keyPressed(KeyEvent lKeyEvent)
                        System.out.println(lKeyEvent.getKeyText(lKeyEvent.getKeyCode()));
              setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
         public static void main(String[] args)
              TestTab lTestTab = new TestTab();
              lTestTab.drawGUI();
              lTestTab.setSize(200, 200);
              lTestTab.setVisible(true);

    The KeyPressed events for TAB and CTRL-TAB are typically consumed by the KeyboardFocusManager for component traversal. What you have to do is adjust traversal keys and add CTRL-TAB as an action of your JTabbedPane. This is usually done with the InputMap/ActionMap, not a KeyListener:
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    public class X {
        public static void main(String[] args) {
            Set tabSet = Collections.singleton(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0));
            KeyStroke ctrlTab = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.CTRL_DOWN_MASK);
            final int FORWARD = KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS;
            final JTabbedPane tp = new JTabbedPane();
            //setFocusTraversalKeys to remove ctrl-tab as a forward gesture
            tp.setFocusTraversalKeys(FORWARD, tabSet);
            for (int i=0; i<6; ++i) {
                JPanel p = new JPanel(new GridLayout(0,1));
                JTextArea area = new JTextArea();
                area.setFocusTraversalKeys(FORWARD, Collections.EMPTY_SET);
                p.add(new JScrollPane(area));
                area = new JTextArea();
                area.setFocusTraversalKeys(FORWARD, Collections.EMPTY_SET);
                p.add(new JScrollPane(area));
                tp.addTab("tab " + i,p);
            //add ctrlTab as an action to move to next tab
            Action nextTab = new AbstractAction("nextTab") {
                public void actionPerformed(ActionEvent evt) {
                    int idx = tp.getSelectedIndex();
                    if (idx != -1) {
                        idx = (idx + 1) % tp.getTabCount();
                        tp.requestFocusInWindow();
                        tp.setSelectedIndex(idx);
            InputMap im = tp.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
            im.put(ctrlTab, nextTab.getValue(Action.NAME));
            tp.getActionMap().put(nextTab.getValue(Action.NAME), nextTab);
            JFrame f= new JFrame("X");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(tp);
            f.setSize(400,300);
            f.setVisible(true);
    [/code[                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Shift+Tab Key Event

    Hallo all,
    I have a big problem ;) I am having JTables and these Jtables must lose their focus when someone clicks on Shift+TAB at the (0,0) th of its cell. I tried to do it in a key event but it doesn't accept my solution. Here is a code chunk:
    AWTEvent currentEvent = EventQueue.getCurrentEvent();
    if( currentEvent instanceof KeyEvent)
            KeyEvent ke = (KeyEvent)currentEvent;
                if(ke.getSource()!=this)
                    return;
                if(rowIndex==getModel().getRowCount()-1 && columnIndex==getModel().getColumnCount()-1
                        && KeyStroke.getKeyStrokeForEvent(ke).equals(shiftTabKeyStroke)){
                    KeyboardFocusManager.getCurrentKeyboardFocusManager().focusPreviousComponent();
                    return;
                }So when I compile it, it makes a return to its 0,0 th cell instead of giving focus on the previous component.

    DB means to say this.
    AWTEvent currentEvent = EventQueue.getCurrentEvent();
    if( currentEvent instanceof KeyEvent)
            KeyEvent ke = (KeyEvent)currentEvent;
                if(ke.getSource()!=this)
                    return;
                if(rowIndex==getModel().getRowCount()-1 && columnIndex==getModel().getColumnCount()-1
                        && KeyStroke.getKeyStrokeForEvent(ke).equals(shiftTabKeyStroke)){
                      SwingUtilities.invokeLater(new Runnable(){
                          public void run()
                                 KeyboardFocusManager.getCurrentKeyboardFocusManager().focusPreviousComponent();                         
                    return;
                }

  • How to handle Shift+Tab key event

    HI,
    This is Ramesh.I have added KeyListener to JTable. I am handling key events through key codes. But I am
    unable to handling multiple key strokes like Shift+Tab
    Can any one please give me suggestion how can I do it
    Thanks & Regards
    Ramesh K

    i dont know about Key BindingsWhich is why you where given a link to the tutorial in the first response.
    can you please give me suggestion.You've been given the suggestion 3 times. You can decide to learn to write code the way other Swing components are written. Or, you can do it the old way. The choice is up to you.

  • How to listen a key event without having any window component

    Hi all,
    I would like to know how can I listen key events from an application that doesn't use windows
    Thanks

    yoiu can make a Thread run inside your application. This will listen for inputs in the command line. For examplethis is part of a chat server (on the console) with let the server manager to enter messages or commands.
    The message or command entered on the console by the user is catched and processed as a String. The string is passed to the processConsole() method, you must define yourself.
    Th Thread is never stopped, so the user may enter any message at any time
    The core of you application should also be a Thread for better performance, but maybe it will also work without. Jst try and provide needed importstatements.
    Runnable runInput = new Runnable() {
            public void run() {
              while(true) {
                try {
                  byte buffer[] = new byte[255];
                  System.out.print(prompt);
                  System.in.read(buffer);//, 0, 255);
                  String inputString = new String(buffer, "Default");
                  processConsole(inputString);
                catch(NullPointerException npe) {
                  System.out.println("Syntax error");
                catch(Exception e) {e.printStackTrace(); }
          Thread inputThread = new Thread(runInput);
          inputThread.start();

  • Removing all tab key events

    Hallo all,
    I am having another problem with my JTables. Because our customer doesn't want to get out of JTable via Ctrl+TAB we have customized the TAB Key behaviour and it skips to the following component and it is fine. But if I go to a cell via TAB key (for example cell(3,4)) and if I select another component above via mouse button and go through the table via TAB key it focuses on the cell (4,4) instead of cell(0,0). How can I solve this problem? How can I refresh or remove the key listeners of the Table after I left the table via mouse? Can anyone help me?

    (Untested, and there may be a better approach)
    I would try adding a FocusListener and in the focusLost, something liketable.getSelectionModel().clearSelection();
    table.getColumnModel().getSelectionModel().clearSelection();luck, db
    edit No that didin't do it. Try this one:table.setRowSelectionInterval(0, 0);
    table.setColumnSelectionInterval(0, 0);Edited by: Darryl.Burke

  • How to make the TAB Key Event called

    Hi ,
    I want to make the tab key called twice when I hit on a button. It should jump to the next 2 cell.
    Thanks.
    DT

    Here's a couple related threads
    http://forum.java.sun.com/thread.jspa?forumID=57&threadID=583877
    http://forum.java.sun.com/thread.jspa?forumID=57&threadID=712386
    I'd also suggest exploring the Search Forums available on the left,
    which is how I found those threads:
    http://onesearch.sun.com/search/onesearch/index.jsp?qt=focus+traversal+tab+text&subCat=siteforumid%3Ajava57&site=dev&dftab=siteforumid%3Ajava57&chooseCat=javaall&col=developer-forums

  • Handling keyTyped events in JTable for TAB key

    In my app, I have a JTable. Some columns are non-editable.
    I have attached a keyListener to the table and have overridden keyTyped() and keyReleased() methods.
    In keyReleased(), I do something depending on the key code. For example: if its the VK_DELETE, I delete the row. If its some other user configured key, then I show a popup dialog where the user can enter some data, etc...
    In keyTyped(), I first check if the column is one of the specific columns. Then I get the keyChar. After this, I show a dialog and pre-populate a JTextField on this dialog with that keyChar.
    My issue is that 'TAB' key events arrive in keyTyped() and not in keyReleased(). As a result, the dialog is shown and the tab takes place inside the JTextfield which is incorrect.
    I would like to ignore TAB keyTyped events. When I look up the keyCode in keyTyped() method, it is 0. So there is no way for me to tell what key was typed.
    How can I ignore TAB events in my keyTyped() method?
    thx

    I now do the following to determine if TAB key has been typed
    public void keyTyped(KeyEvent e) {
    char c = e.getKeyChar();
    if (c == KeyEvent.VK_TAB) {
    Is this correct ?
    Edited by: tsc on Sep 28, 2007 12:40 PM

  • How to alter behaviour of pressing enter key to tab key?

    Hi! I want to alter the behaviour when pressing the enter key so it acts like pressing the tab key in a JTextField. That is pressing the enter key will transfer the cursor to the next field instead of the cursor just staying in the current field.
    Thanks in advance.
    rralilin

    Hi,
    there are more than one way to do this:
    1. use an ActionListener and transfer the focus in its actionPerformed(...) method - good for one or a few JTextFields - if there are more, try ...
    2. prelisten the key events via the new KeyboardFocusManager und replace the corresponding key event by a Tab-key-event and redispatch it - this approach enables you, to handle this new behavior for all JTextFields in one central position - possible since JDK 1.4
    Hope, this helps
    greetings Marsian

  • Field validation with Tab Key

    I am writing an application in which I must validate the data a user enters into a textfield when the user presses the "Tab" key. I have added a KeyListener to the textfield and when this java code is run as an applet the KeyListener catches the Tab key and all works fine. When I run this as an application, the Tab key does not seem to be caught at all by the Key Listener.
    Has anybody else had this problem on the IPAQ, if so, do you know a solution.
    Any help would be GREATLY appreciated.

    I've been having this problem as well. I have created my applet using Visual Cafe 3 (Java 1.1). When I run this in Jeode it work great, Personal java works great, MSVM works great, but when I try to run using Sun Plugin 1.4.2_02 the tab key events are never fired.
    Does anybody have a solution for this? Unfortunately, we are stuck developing with an older version of Java because of Visual Cafe.

  • Adf javascript clientlistener not capturing the TAB keypress event

    Hi,
    I have client listener attached to a text field to trap the keyPress event. The method is being called for all the key strokes except special keys like TAB, arrow keys, pageup, pagedown etc. Is there any possibility to capture TAB key event.
    <af:clientListener method="myFunctionTosetFocus"
    type="keyPress"/>
    function myFunctionTosetFocus(event) {
    alert('function called....')
    var keyCodePressed = event.getKeyCode();
    if(keyCodePressed == AdfKeyStroke.ENTER_KEY || keyCodePressed == AdfKeyStroke.TAB_KEY) {
    alert('Do some thing here....')
    I can see both the alert messages when I click on the ENTER key. But I cannot see both the alert messages when I cick on TAB key. The same is working without any problems in firefox.
    Thanks and Regards,
    S R Prasad

    Hi Frank,
    I tried with the event.cancel(). Now I can see the event.cancel() working for ENTER key, But not for the TAB key. I tried with the keyDown event in the clientListener and it is working with out any problems in both IE and firefox.
    It is not working as expected in case of keyUp event, If I set the focus to the next tab in the clientListener code, it is executing the clientListener of the destination component if the clientListener event is keyUp.
    Overall it is wotking as expected with the keyDown, But it is not working for the keyUp and keyPress events. All these are working as expected in firefox.
    Thanks and Regards,
    S R Prasad

  • Getting key events without a jcomponent...

    Is it possible to get key events without adding a keylistener to a jpanel? I want a class of mine to manage input of it's own, but it has no reference to a jpanel. They don't necessarily have to be KeyEvents, but just some way to detect if a key has been pressed (like the arrow keys). How can I do this? Thanks.

    Lots of components can listen for key events.
    What does your class subclass?It doesn't subclass anything. I am creating a custom menu system for my game using images I have made for the tileset. I would like it to handle some keyboard events of its own. (Like if the down arrow is pressed, it will move the focus down to the next component on its own). Right now I am currently passing the key events from my fullscreen jpanel to the menu class and it is handling them that way. Thanks.

Maybe you are looking for