Traversing Focus Using Enter Key

Please Try to Help Me on this ............

I Have a Screen where I need to traverse focus (it has textfields, combo boxes and buttons) when I press the Enter Key (like Tab).

I used a Key listener and was able to do it (to a certain point)

The code I have written is below ..

Now The problem is when the focus sets on a JComboBox, it wont go further (tab works). I know that the enter key in a JComboBox is used to select a item.

So what I need is first to be able to select an item using the enter key and when press enter key again, the focus must go to the next component.

Is there a way to do it ?

(JCheckBox and JButton has similar problems)

SymKey aSymKey = new SymKey();

textfield1.addKeyListener(aSymKey);

textfield2.addKeyListener(aSymKey);

combobox1.addKeyListener(aSymKey);

combobox2.addKeyListener(aSymKey);

checkbox1.addKeyListener(aSymKey);

textfield3.addKeyListener(aSymKey);

button1.addKeyListener(aSymKey);

class SymKey extends KeyAdapter {

public void keyPressed(KeyEvent e){

if (e.getKeyCode() == e.VK_ENTER) {

if (e.isShiftDown())

FocusManager.getCurrentManager().focusPreviousComponent((JComponent)e.getSource());

else

FocusManager.getCurrentManager().focusNextComponent((JComponent)e.getSource());

}

}

}

[1381 byte] By [shirantha9999] at [2007-9-27 22:44:17]
# 1
Maybe you have to generate your own button via extends AbstractButton and override themethod addKeyListener(eve), same as others.
tigertdf at 2007-7-7 13:42:18 > top of Java-index,Archived Forums,Java Programming...
# 2

I do it this way:

Extend JCheckBox (or whatever) and implement KeyListener.

Don't forget to call addKeyListener(this) (in the constructor for example)

then implement KeyListener as follows:

// -- KeyListener interface -

public void keyPressed(KeyEvent e) {

if (e.getKeyCode() == KeyEvent.VK_ENTER) {

transferFocus();

e.consume();

}

}

public void keyReleased(KeyEvent e) {}

public void keyTyped(KeyEvent e) {}

Hope that helps !

Best regards, Peter.

peterjava at 2007-7-7 13:42:18 > top of Java-index,Archived Forums,Java Programming...