Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mapping ENTER as a Tab Event

Status
Not open for further replies.

Watts

Programmer
Jan 18, 2001
80
US
How do using the ActionListener map the ENTER key as a TAB event? Effectively I want to generate a TAB keypressed when the user presses enter on a TextField.
 
Try just setting the focus to the next component on an enter key event via the requestFocus function.
Your component does have to know its successor for that though, which makes this solution actually a workaround when thinking OOP :)I - maybe somebody knows a better one:

this.addKeyListener(new KeyListener()
{
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_TAB)
{
nextComponent.requestFocus();
}
}
});

Hope this helps
(note: if it does not compile or work: I did not test it #-))...
allow thyself to be the spark that lights the fire
haslo@haslo.ch - www.haslo.ch​
 
Hi Watts,

You can also set keycodes directly:

Panel p = new Panel(new GridLayout(1,2));
TextField tf = new TextField();
p.add(tf);

tf.addKeyListener(new KeyListener()
{
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_ENTER)
{
e.setKeyCode(KeyEvent.VK_TAB);
}
}

public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
});

Now when you press Enter at TextField it's mapped to TAB.

I hope that helped you.

-Vepo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top