import java.awt.Component;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.HashSet;
import java.util.Set;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
public class NewJApplet extends javax.swing.JApplet implements KeyListener {
public void init() {
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
initListeners();
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void initListeners() {
Set forwardKeys = this.getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
Set newForwardKeys = new HashSet(forwardKeys);
newForwardKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0));
this.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, newForwardKeys);
jTextField1.addKeyListener(this);
jTextArea1.addKeyListener(this);
jTextField1.setSelectionStart(0);
jTextField1.setSelectionEnd(jTextField1.getText().length());
}
private void initComponents() {
jTextField1 = new javax.swing.JTextField();
jScrollPane2 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jLabel1 = new javax.swing.JLabel();
getContentPane().setLayout(new java.awt.FlowLayout());
jTextField1.setText("Jose");
jTextField1.setNextFocusableComponent(jTextArea1);
getContentPane().add(jTextField1);
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jTextArea1.setText("TAB and ENTER keys\nwill traverse the\nfields.");
jTextArea1.setNextFocusableComponent(jTextField1);
jScrollPane2.setViewportView(jTextArea1);
getContentPane().add(jScrollPane2);
jLabel1.setText("Press TAB or ENTER Key.");
jLabel1.setFocusable(false);
jLabel1.setRequestFocusEnabled(false);
getContentPane().add(jLabel1);
}
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
switch(e.getKeyCode()) {
case KeyEvent.VK_TAB:
case KeyEvent.VK_ENTER:
Component component = e.getComponent();
if (component instanceof JTextArea) {
JTextArea tmpJTextArea = (JTextArea) component;
e.consume();
tmpJTextArea.getNextFocusableComponent().requestFocusInWindow();
}
break;
}
}
public void keyReleased(KeyEvent e) {
}
}