Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
How to handle the text using Key Listener Interface 
 

In this section, you will learn how to handle the text using the key events on the Java Awt component.

 

How to handle the text using Key Listener Interface

                         

In this section, you will learn how to handle the text using the key events on the Java Awt component. All the key events are handled through the KeyListener Interface. In the given example, we are going to show you how to display the text of textField1 on the text field2 on key pressed. 

Given is the description of example code:
In the example, we have create two text fields text1 and text2. When you press the key to write the text on the text1  then you will see the same text will start displaying on the text2. This is possible by using the methods keyPressed() and keyTyped().

Here is the code of TextCopied.java

import java.awt.*;
import javax.swing.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
 
public class TextCopied extends JFrame {
    public TextCopied() throws HeadlessException {
        initComponents();
    }
  protected void initComponents() {
       
        setSize(200200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
 
        JLabel label1 = new JLabel("TextField1: ");
        final JTextField text1 = new JTextField();
    JLabel label2 = new JLabel("TextField2: ");
        final JTextField text2 = new JTextField();
        text1.setPreferredSize(new Dimension(10020));
    text2.setPreferredSize(new Dimension(10020));
        getContentPane().add(label1);
        getContentPane().add(text1);
    getContentPane().add(label2);
        getContentPane().add(text2);
 
        text1.addKeyListener(new KeyAdapter() {
            public void keyReleased(KeyEvent e) {
             String text = text1.getText();
        if(text.equals("")){
          System.out.println("Enter the value");
        }
        else{

                text2.setText(text);
        }
            }
            public void keyTyped(KeyEvent e) {
            }
            public void keyPressed(KeyEvent e) {
            }
        });
    }
      public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new TextCopied().setVisible(true);
            }
        });
    }
}

Output will be displayed as:

Download Source Code:

                         

» View all related tutorials
Related Tags: java c ide class graphics constructor object io struct classes size 2d graph const ip vi trac awt int this

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.