0% found this document useful (0 votes)
2 views5 pages

Action Event

The document outlines various listener interfaces in Java, including ActionListener, ItemListener, MouseListener, MouseMotionListener, KeyListener, and TextListener, along with their respective methods. It provides example implementations of these listeners in Java Swing applications, demonstrating how to handle different types of events such as action events, item state changes, text changes, key presses, and mouse actions. Each example includes a simple GUI setup and the corresponding event handling logic.

Uploaded by

aditishirole2112
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Action Event

The document outlines various listener interfaces in Java, including ActionListener, ItemListener, MouseListener, MouseMotionListener, KeyListener, and TextListener, along with their respective methods. It provides example implementations of these listeners in Java Swing applications, demonstrating how to handle different types of events such as action events, item state changes, text changes, key presses, and mouse actions. Each example includes a simple GUI setup and the corresponding event handling logic.

Uploaded by

aditishirole2112
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Listener Interfaces and Their Methods

Listener Interface Methods

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();
}
}

You might also like