Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to intercept input to JTextArea, JTextField, JTextPane, ...

mohsenmadiJan 31 2007 — edited Feb 4 2007
Dear all -

How can one intercept the input coming from a keyboard before it is sent and displayed on some JTextComponent object (e.g., JTextPane)? For example, if you run the program below as is (built on Netbeans 5.5), anything you type will show on the JTextPane as expected; press the keys 'h', 'e', 'l', 'l', 'o', and you get "hello" on the JTextPane object. However, I need to intercept such typical behavior so that each key typed on the keyboard is mapped to whatever the application maps it to. For example, typing 'h', 'e', 'l', 'l', 'o' could result in the string "ifmmp", which is non other than mapping each character to the character next to it (Caesar cipher), or, typing 'h', 'e', 'l', 'l', 'o' may result in the string "HELLO" being displayed instead, where the mapping is simply to replace each character by it's uppercase equivalent.

I tried the obvious thing that may come to one's mind: add a key-press event handler to transform the keys I type before they are displayed on the jTextPanel, but by the time I get into that event handler, it's too late as the character typed are already shown therein. I could of course manipulate the data shown by executing methods such as getText(), replaceText(), setText() ..., but these are not elegant solutions as you may agree.

This is possible. Take a look at http://imtranslator.net/keyboard.asp for example. Change the pull-down menu value of the rendered Virtual Keyboard from English to even Arabic or Hebrew for example and see the immediate result! That's interception, right? How can I achieve a similar (and much simpler) functionality on JTextComponents? Pleeeeeeeeeeeease?

Thank for your time very much,
Mohsen

/*
 * TextInterceptor.java
 *
 * Created on January 31, 2007, 10:22 PM
 */

public class TextInterceptor extends javax.swing.JFrame {
    
    /** Creates new form TextInterceptor */
    public TextInterceptor() {
        initComponents();
    }
    
    private void initComponents() {
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextPane1 = new javax.swing.JTextPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jTextPane1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextPane1KeyPressed(evt);
            }
        });

        jScrollPane1.setViewportView(jTextPane1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pack();
    }

    private void jTextPane1KeyPressed(java.awt.event.KeyEvent evt) {
        // Apparently, by the time I get here, it is too late to do
        // any changes to the key I typed, as it is already shown on 
        // the jTextPanel.
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TextInterceptor().setVisible(true);
            }
        });
    }
    
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextPane jTextPane1;
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 4 2007
Added on Jan 31 2007
13 comments
1,097 views