Friday, 4 April 2014

TextListener



public interface TextListener extends EventListener {
    public void textValueChanged(TextEvent e);
}

void textValueChanged(TextEvent e)
Invoked when the value of the text has changed. The code written for this method performs the operations that need to occur when text changes.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

public class TextListenerEx{
 JTextArea displayArea;
 TextField typingArea;
 JFrame frame1;
  
 TextListenerEx(){
  
  /* Initialize typingArea */
  typingArea = new TextField(20);
  
  /* Initialize Display Area */
  displayArea = new JTextArea();
  displayArea.setEditable(false);
  JScrollPane scrollPane = new JScrollPane(displayArea);
  scrollPane.setPreferredSize(new Dimension(150, 200));
   
  /* Initialize Frame */
  frame1 =new JFrame("Sample Frame");
  frame1.pack();
  frame1.setVisible(true);
  frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame1.getContentPane().add(typingArea, BorderLayout.PAGE_START);
  frame1.getContentPane().add(scrollPane, BorderLayout.CENTER);
  frame1.setSize(300, 500);
    
  /* Add Key Listener */
  typingArea.addTextListener(new TextListener(){
   public void textValueChanged(TextEvent e){
    displayArea.append(typingArea.getText() +"\n");
   } 
  });   
 }
 
 public static void main(String args[]){
  new TextListenerEx();
 }
}

Output




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment