Results 1 to 3 of 3
  1. #1
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default How Enter key act like TAB?

    Hi, I would like to know how to make enter key act like tab. When user press Enter key then the focus will be transfer to the next control. I have tried things but I cannot get the right thing. Please dont get me worng but I think my past programming in VB6 affect me now, my thinking, how to code this in java.

    Here is the latest code I am trying right now:
    Java Code:
    public class frmColors extends javax.swing.JFrame implements FocusListener, KeyListener{
    ....
    
     Object nextfocusIs = null;  //will hold the control that has the focus
    
        public void focusGained(FocusEvent e)
        {   if(e.getSource() instanceof JTextField)
            {FormUtilities.highLightText((JTextField) e.getSource());}
            nextfocusIs = e.getSource();
        }
    
        public void focusLost(FocusEvent e) {}
    
    
        public void keyTyped(KeyEvent e) {}
    
        public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_ENTER)
            {   transferFocus();
            }
        }
    
        public void keyReleased(KeyEvent e)
        {   
        }
    When I type nextfocusIs. it only shows methods:
    equals()
    getClass()
    hashCode()
    notify()
    notifyAll()
    toString()
    wait()

    Thanks in advance,
    geje
    Last edited by mine0926; 06-03-2010 at 04:21 AM.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    Read the section from the Swing tutorial on How to Use the Focus Subsystem for the solution.

  3. #3
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Thanks. I use this codes.

    Java Code:
            if(getFocusOwner() instanceof JButton)
            {   if (e.getKeyCode() == KeyEvent.VK_DOWN)
                {getFocusOwner().transferFocus();}
                if (e.getKeyCode() == KeyEvent.VK_UP)
                {getFocusOwner().transferFocusBackward();}
            }
            Else
            {   if (e.getKeyCode() == KeyEvent.VK_DOWN)
                {getFocusOwner().transferFocus();}
                if (e.getKeyCode() == KeyEvent.VK_ENTER)
                {getFocusOwner().transferFocus();}
                if (e.getKeyCode() == KeyEvent.VK_UP)
                {getFocusOwner().transferFocusBackward();}
            }

Similar Threads

  1. Enter
    By Arnold in forum New To Java
    Replies: 8
    Last Post: 10-18-2009, 09:17 AM
  2. enter key in swt
    By ashin in forum SWT / JFace
    Replies: 0
    Last Post: 07-17-2009, 09:42 AM
  3. Detect the ENTER key being pressed
    By aaronfsimons in forum New To Java
    Replies: 12
    Last Post: 05-16-2009, 08:48 PM
  4. what code for not hitting enter?
    By tornbacchus in forum New To Java
    Replies: 4
    Last Post: 04-11-2009, 04:59 AM
  5. enter key problem
    By masa in forum AWT / Swing
    Replies: 3
    Last Post: 03-19-2009, 07:30 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
  •