< Prev
Next >





TextEvent and TextListener





An event of type TextEvent is generated when a value in textfield or textarea is entered or edited.




Method of TextEvent class


Methods Description
public String paramString() Returns the String describing the TextEvent.






A class to listen and respond to a TextEvent, must perform the two steps:




Methods Description
public void addTextListener(TextListener object) where object is an object of the class that has implemented TextListener interface. Doing this, registers the class to listen and respond to mouse move and drag TextEvent.






Handling an TextEvent in a TextField by implementing TextListener interface


In this upcoming code, we are going to listen to TextEvent in a textfield, by implementing the TextListener interface.

import java.awt.*;
import java.awt.event.*;


public class TextEventEx1 implements TextListener
{
Label label1, label2;
TextField field1;
Frame jf;
String str;


TextEventEx1()
{

jf = new Frame("Handling TextEvent");

label1= new Label("Type in the textfield, to see the textevents it generates -", Label.CENTER);
label2= new Label();
field1 = new TextField(25); 

jf.setLayout(new FlowLayout());

jf.add(label1);
jf.add(field1);
jf.add(label2);

//Registering the class TextEventEx1 to catch and respond to mouse text events 
field1.addTextListener(this); 

jf.setSize(340,200);
jf.setVisible(true);
}


public void textValueChanged(TextEvent te)
{
label2.setText(te.paramString());
jf.setVisible(true);
}

public static void main(String... ar)
{
new TextEventEx1();
}

}
When you run the code, you are presented a window that shows an empty textfield.

Figure 1



When this textfield is active and if you type in any character or digit, you are notified about the change in textfield value by a constant TEXT_VALUE_CHANGED of TextEvent class.

Figure 2





Handling an TextEvent in a TextArea by implementing TextListener interface


In this upcoming code, we are going to listen to TextEvent in a textarea, by implementing the TextListener interface.

import java.awt.*;
import java.awt.event.*;


public class TextEventEx2 implements TextListener
{
Label label1, label2;
TextArea area1;
Frame jf;
String str;


TextEventEx2()
{

jf = new Frame("Handling TextEvent");

label1= new Label("Type in the TextArea, to see the TextEvent -", Label.CENTER);
label2= new Label();


area1 = new TextArea(5,20); //calling TextField(String)

jf.setLayout(new FlowLayout());

jf.add(label1);
jf.add(area1);
jf.add(label2);

//Registering the class TextEventEx2 to catch and respond to mouse text events 
area1.addTextListener(this);

jf.setSize(300,200);
jf.setVisible(true);
}

public void textValueChanged(TextEvent te)
{
label2.setText(te.paramString());
jf.setVisible(true);
}

public static void main(String... ar)
{
new TextEventEx2();
}

}
When you run the code, you are presented a window that shows an empty textarea.

Figure 3



When this textarea is active and if you type in any character or digit, you are notified about the change in textarea's value by a constant TEXT_VALUE_CHANGED of TextEvent class.

Figure 4




Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter




< Prev
Next >
< AdjustmentListener
Frame >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
C++ and Python

Ad2