How do I add key listener event handler to JTextField?

Category: javax.swing, viewed: 27003 time(s).

In this small swing example we demonstrate how to use KeyAdapter class to handle keyboard event for the text field component to change the text-field's text to uppercase.

package org.kodejava.example.swing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class UppercaseTextFieldDemo extends JFrame {
    public UppercaseTextFieldDemo() throws HeadlessException {
        initComponents();
    }

    protected void initComponents() {
        //
        // Set default form size, closing event and layout manager
        //
        setSize(200, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));

        //
        // Create a label and text field for our demo application and add the
        // component to the frame content pane object.
        //
        JLabel usernameLabel = new JLabel("Username: ");
        JTextField usernameTextField = new JTextField();
        usernameTextField.setPreferredSize(new Dimension(100, 20));
        getContentPane().add(usernameLabel);
        getContentPane().add(usernameTextField);

        //
        // Register a KeyListener for the text field. Using the KeyAdapter class
        // allow us implement the only key listener event that we want to listen,
        // in this example we use the keyReleased event.
        //
        usernameTextField.addKeyListener(new KeyAdapter() {
            public void keyReleased(KeyEvent e) {
                JTextField textField = (JTextField) e.getSource();
                String text = textField.getText();
                textField.setText(text.toUpperCase());
            }

            public void keyTyped(KeyEvent e) {
                // TODO: Do something for the keyTyped event
            }

            public void keyPressed(KeyEvent e) {
                // TODO: Do something for the keyPressed event
            }
        });
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                //
                // Create a new instance of our application and display it.
                //
                new UppercaseTextFieldDemo().setVisible(true);
            }
        });
    }
}
Click here to lend your support to: Kode Java Org and make a donation at www.pledgie.com !

 

Uncensored Newsgroups
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Java Training

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats