TextEvent


Prev Tutorial Next Tutorial

Event Handling for TextField

TextEvent class and TextListener interface are associated with both textfield and textarea. If we want to performed any operation, if the value of textfield and textarea are changed than the logic should be written in the textValueChanged().

GUI ComponentEvent classListener InterfaceMethod (abstract method)
TextFieldTextEventTextListenerpublic void textValueChanged(TextEvent e)

Example of TextEvent

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

class TextEventEx extends Frame implements TextListener
{
TextField tf1,tf2,tf3;
Label l1,l2,l3;
String nm="",xc="",yc="";
int x,y;
Panel p1;
TextEventEx()
{		
setBackground(Color.cyan);
p1=new Panel();
p1.setBackground(Color.yellow);
l1=new Label("Name");
l2=new Label("X-axies");
l3=new Label("y-aixes");

tf1=new TextField(15);
tf2=new TextField(5);
tf3=new TextField(5);
		    
p1.add(l1);
p1.add(tf1);
p1.add(l2);
p1.add(tf2);
p1.add(l3);
p1.add(tf3);
add("North",p1);
		
tf1.addTextListener(this);
tf2.addTextListener(this);
tf3.addTextListener(this);
}

public void textValueChanged(TextEvent e)
{
nm=tf1.getText();
xc=tf2.getText();
x=Integer.parseInt(xc);
yc=tf3.getText();
y=Integer.parseInt(yc);
repaint();
}

public  void paint(Graphics g)
{
setFont(new Font("TimesRoman",Font.BOLD,30));
g.setColor(Color.red);
g.drawString("Name"+nm,x,y);
}

public static void main(String args[])
{
TextEventEx t=new TextEventEx();
Toolkit tx=Toolkit .getDefaultToolkit();
t.setSize(tx.getScreenSize());
t.setVisible(true);
}
}

Download code Click

textevent example
Prev Tutorial Next Tutorial

Advertisements




Facebook Likes

Advertisements