key Traverse « Event « Java Swing Q&A





1. Traversing using a KeyListener    coderanch.com

import java.awt.event.KeyEvent; import javax.swing.JSpinner; import javax.swing.JTextField; public class Main extends javax.swing.JFrame implements java.awt.event.KeyListener { public Main() { add(first = new JSpinner(), java.awt.BorderLayout.NORTH); ((javax.swing.JSpinner.DefaultEditor)first.getEditor()).getTextField().setFocusTraversalKeysEnabled(false); ((javax.swing.JSpinner.DefaultEditor)first.getEditor()).getTextField().addKeyListener(this); add(second = new JTextField("Java"), java.awt.BorderLayout.SOUTH); second.setFocusTraversalKeysEnabled(false); second.addKeyListener(this); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(new java.awt.Dimension(50, 100)); setVisible(true); } public static void main(String[] args) { new Main(); } public void keyPressed(KeyEvent e) { if (e.getKeyCode() == 9) if (e.getComponent().equals(((javax.swing.JSpinner.DefaultEditor)first.getEditor()).getTextField())) { second.requestFocusInWindow(); second.selectAll(); } ...