Action Event
Action Event
ActionListener actionPerformed(ActionEvent e)
ItemListener itemStateChanged(ItemEvent e)
MouseListener mouseClicked(MouseEvent
e)mousePressed(MouseEvent
e)mouseReleased(MouseEvent
e)mouseEntered(MouseEvent
e)mouseExited(MouseEvent e
MouseMotionListener mouseMoved(MouseEvent e)
mouseDragged(MouseEvent e)
KeyListener keyPressed(KeyEvent e)
keyReleased(KeyEvent e)
keyTyped(KeyEvent e)
TextListener textValueChanged(TextEvent e)
Action Event
package AWT;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Event1 extends JFrame implements ActionListener{
JButton b;
public Event1()
{
setLayout(new FlowLayout());
setSize(300,300);
b = new JButton("Click Button !");
b.addActionListener(this);
add(b);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
b.setText("Clicked");
}
public static void main(String[] args)
{
new Event1();
}
}
Item Event
package AWT;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Event1 extends JFrame implements ItemListener{
JCheckBox c;
public Event1()
{
setLayout(new FlowLayout());
setSize(300,300);
c=new JCheckBox("Check this");
c.addItemListener(this);
add(c);
setVisible(true);
}
public void itemStateChanged(ItemEvent e)
{
System.out.println("Selectd !");
}
public static void main(String[] args)
{
new Event1();
}
}
TextEvent
package AWT;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Event1 extends JFrame implements TextListener{
TextField t;
public Event1()
{
setLayout(new FlowLayout());
setSize(300,300);
setVisible(true);
JLabel ll = new JLabel("Enter name : ");
t = new TextField(30);
t.addTextListener(this);
add(ll);
add(t);
}
public void textValueChanged(TextEvent e)
{
System.out.println("You typed : "+t.getText());
}
public static void main(String[] args)
{
new Event1();
}
}
Key Event
package AWT;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Event1 extends JFrame implements KeyListener{
Label l;
public Event1()
{
setLayout(new FlowLayout());
setSize(300,300);
l = new Label("Press a Key !");
TextField t = new TextField();
t.addKeyListener(this);
add(l);
add(t);
setVisible(true);
}
public void keyPressed(KeyEvent e)
{
System.out.println("You pressed : "+e.getKeyChar());
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
public static void main(String[] args)
{
new Event1();
}
}
MouseEvent
package AWT;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Event1 extends JFrame implements MouseListener{
Label l;
public Event1()
{
setLayout(new FlowLayout());
setSize(300,300);
l = new Label("Click ANywhwere !");
addMouseListener(this);
add(l);
setVisible(true);
}
public void mouseClicked(MouseEvent e)
{
System.out.println("You clicked at : "+e.getX()+" ,
"+e.getY());
}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public static void main(String[] args)
{
new Event1();
}
}
MouseMotion Event
package AWT;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Event1 extends JFrame implements MouseMotionListener
{
Label l;
public Event1()
{
setLayout(new FlowLayout());
setSize(300,300);
l=new Label("Mov mouse : ");
addMouseMotionListener(this);
setVisible(true);
}
public void mouseMoved(MouseEvent e)
{
System.out.println("Mouse Moved to : "+e.getX()+" ,
"+e.getY());
}
public void mouseDragged(){}
public static void main(String[] args) {
new Event1();
}
}