/*
 * NewJApplet.java
 *
 * Created on November 20, 2007, 2:26 AM
 */

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;

/**
 *
 * @author  jose
 */
public class NewJApplet extends javax.swing.JApplet implements KeyListener {
    
    /** Initializes the applet NewJApplet */
    public void init() {
        try {
            java.awt.EventQueue.invokeAndWait(new Runnable() {
                public void run() {
                    initComponents();
                    initListeners();
                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    private void initListeners() {
        // Take care of ENTER key
        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);
        
        // Register KeyListeners for fields
        jTextField1.addKeyListener(this);
        jTextArea1.addKeyListener(this);
        
        jTextField1.setSelectionStart(0);
        jTextField1.setSelectionEnd(jTextField1.getText().length());        
    }
    
    /** This method is called from within the init() method to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    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);

    }// </editor-fold>//GEN-END:initComponents
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
    
    public void keyTyped(KeyEvent e) {
    }
    
    // This is where the events get handled
    // Note that you want keyPressed() because it's the first event handler called after
    // a KeyEvent happens. And it has to be first one, because we need to consume the event
    public void keyPressed(KeyEvent e) {
        switch(e.getKeyCode()) {
            case KeyEvent.VK_TAB:
            case KeyEvent.VK_ENTER:
                // Handle event for JTextArea fields
                Component component = e.getComponent();
                
                if (component instanceof JTextArea) {
                    JTextArea tmpJTextArea = (JTextArea) component;
                    
                    // First consume the key press event, i.e., ignore the key event
                    e.consume();
                    
                    // Then go to the next field
                    // Note that getNextFocusableComponent() is deprecated, but I could not find any other way of doing this
                    tmpJTextArea.getNextFocusableComponent().requestFocusInWindow();
                }
                
                break;
        }
    }
    
    public void keyReleased(KeyEvent e) {
    }
    
}
Syntax Highlighting created using the com.Ostermiller.Syntax package.
Tuesday, November 20 2007 at 03:23