How you can navigate between JComponents(JTextFields,JComboBox etc) using arrow keys or Enter key?

Nicholas Whitehead

You need to catch the key code code of the KeyTyped event. Then inspect the value (Enter is 10) and if it is the "trigger" value, move the focus to the next compnent.

    // This adds a listener to the test field txtTicker
    txtTicker.addKeyListener(new java.awt.event.KeyAdapter() {
      public void keyTyped(KeyEvent e) {
  // This method is fired whenever a key is typed in txtField
  // If the code is 10 (Enter), then the focus moves to txtMaturity
  void txtTicker_keyTyped(KeyEvent e) {
  if(e.getKeyCode()==10) txtMaturity.requestFocus();

If you need a quickie reference to what the code keys are, just System.out the key code for a field and you will see what they are.

0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact