Pages

Event Handling in java....

Changing the state of an object is known as an event. For example, click on button, dragging mouse etc. The java.awt.event package provides many event classes and Listener interfaces for event handling.


Event classes Listener Interfaces
ActionEvent ActionListener
MouseEvent MouseListener and MouseMotionListener
MouseWheelEvent MouseWheelListener
KeyEvent KeyListener
ItemEvent ItemListener
TextEvent TextListener
AdjustmentEvent AdjustmentListener
WindowEvent WindowListener
ComponentEvent ComponentListener
ContainerEvent ContainerListener
FocusEvent FocusListener


Steps to perform Event Handling:

Following steps are required to perform event handling:
  1. Implement the Listener interface and overrides its methods
  2. Register the component with the Listener. For registering the component with the Listener, many classes provides the registration methods. For example:
    • Button : public void addActionListener(ActionListener a){}
    • MenuItem : public void addActionListener(ActionListener a){}
    • TextField : public void addActionListener(ActionListener a){}                                                                 public void addTextListener(TextListener a){}
    • TextArea public void addItemListener(ItemListener a){}
    • List : public void addActionListener(ActionListener a){}
                public void addItemListener(ItemListener a){}


  3. Event Handling Codes:
    We can put the event handling code into one of the following places:

    1. Same class
    2. Other class
    3. Anonymous class



ActionEvent

Event Fired by Listener interface Handler method
java.awt.event.ActionEvent JButton
JCheckBoxMenuItem
JComboBox
JFileChooser
JList
JRadioButtonMenuItem
JTextField
JToggleButton
ActionListener actionPerformed()


Example of ActionEvent

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

class AEvent extends JFrame implements ActionListener
{
JTextField tf;
AEvent()
{
tf = new JTextField();
tf.setBounds(60,50,170,20);

JButton b = new JButton("click me!");
b.setBounds(100,100,100,30);
b.addActionListener(this);

add(b);
add(tf);
setSize(400,300);
setTitle("ActionEvent Example");
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

public void actionPerformed(ActionEvent e)
{
tf.setText("Welcome..");
JButton b1 = new JButton("Don't click");
add(b1);
b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
JTextField tf1 = new JTextField();
tf1.setBounds(60,160,250,30);
tf1.setForeground(Color.red);
tf1.setFont(new Font("serief",Font.BOLD,15));
tf1.setText("Welcome to hungry4java.blogspot.com");
add(tf1);
}
});
b1.setBounds(100,200,100,30);
}

public static void main(String[] args)
{
new AEvent();
}
}



ItemEvent





Event Fired by Listener interface Handler method
java.awt.event.ItemEvent   JCheckBoxMenuItem
JComboBox
JRadioButtonMenuItem
JToggleButton
ItemListener ItemStateChanged()


Example of ItemEvent

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

class ItemEventDemo
{
JFrame f;
ItemEventDemo()
{
f = new JFrame("Combo Example");
String country[] = {"yellow","cyan","blue","red"};
final JComboBox cb = new JComboBox(country);
cb.setBounds(50,50,90,20);
f.add(cb);
f.setLayout(null);
f.setSize(400,500);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

cb.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
int index = cb.getSelectedIndex();
if(index == 0)
f.getContentPane().setBackground(Color.yellow);
if(index == 1)
f.getContentPane().setBackground(Color.cyan);
if(index == 2)
f.getContentPane().setBackground(Color.blue);
if(index == 3)
f.getContentPane().setBackground(Color.red);
}
});
}

public static void main(String[] args)
{
new ItemEventDemo();
}
}



AdjustmentEvent


Event Fired by Listener interface Handler method
java.awt.event.AdjustmentEvent   JScrollBar  AdjustmentListener adjustmentValueChanged()


Example of AdjustmentEvent

import java.applet.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class AdjustmentEventDemo extends JFrame implements AdjustmentListener
{
JScrollBar r,g,b;
JPanel p;

AdjustmentEventDemo()
{
setLayout(null);

p = new JPanel();
p.setBounds(50,50,500,60);
r = new JScrollBar(0,1,20,1,275);
g = new JScrollBar(0,1,20,1,275);
b = new JScrollBar(0,1,20,1,275);
p.setLayout(new GridLayout(3,1));
p.add(r);
p.add(g);
p.add(b);
add(p,"North");
r.addAdjustmentListener(this);
g.addAdjustmentListener(this);
b.addAdjustmentListener(this);

setSize(700,300);
setTitle("Set your own color..");
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void adjustmentValueChanged(AdjustmentEvent e)
{
int cr,cg,cb;
cr = r.getValue();
cg = g.getValue();
cb = b.getValue();
getContentPane().setBackground(new Color(cr,cg,cb));
}

public static void main(String [] args)
{
new AdjustmentEventDemo();
}
}



KeyEvent


Event Fired by Listener interface Handler method
java.awt.event.KeyEvent   All component   KeyListener KeyTyped()
KeyPressed()
KeyReleased()


Example of KeyEvent

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

class KeyEventDemo extends JFrame
{
JLabel l1;
JTextField tf;
JTextArea ta;
JScrollPane sp;

KeyEventDemo()
{
setLayout(null);

l1 = new JLabel("Enter a text");
l1.setBounds(50,50,100,30);
tf = new JTextField(20);
tf.setBounds(170,50,150,30);

ta =new JTextArea (10,10);
sp = new JScrollPane(ta);

ta.setBounds(170,100,150,150);
add(l1);add(tf);add(ta);

setSize(300,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("KeyEvent Example");
tf.addKeyListener(new KeyAdapter()
{
public void keyTyped(KeyEvent e)
{
int x = e.getKeyChar();
ta.append(""+x);
}
});
}

public static void main(String[] args)
{
new KeyEventDemo();
}
}




Adapter class

In java.awt.event.* we have Listener interface called WindowListener which contains seven abstract methods. In the derived class implements WindowListener interface; it is mandatory for derived class to define all the methods even though the derived class is not required. If the derived class wants to define the required methods, it has to extend its corresponding adapter class called java.awt.event.WindowAdapter and this class contains null body deinition for WindowListener Therefore which  Listener interface contains more than one undefined method for that Listener interfaces have the appropriate class whose general notation is XXXAdapter.

Listener Adapter
WindowListener WindowAdapter
KeyListener KeyAdapter
MouseListener MouseAdapter
MouseMotionListener  MouseMotionAdapter 

MouseListener and MouseMotionListener


Event Fired by Listener interface Handler method
java.awt.event.MouseEvent   All component   MouseListener
MouseMotionListener 
mouseClicked()
mousePressed()
mouseReleased()
mouseEntered()
mouseExisted()
mouseDragged()
mouseMoved()


Example of MouseEvent

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

class MouseEventDemo extends JFrame
{
int x1,y1,x2,y2;
MouseEventDemo()
{
setSize(300,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.addMouseListener(new MouseAdapter()
{
@Override
public void mouseClicked(MouseEvent e)
{
Graphics g = getGraphics();
x1 = e.getX();
y1 = e.getY();
g.drawString("The position you have clicked is : "+x1+" "+y1,100,100);
}
});
this.addMouseMotionListener(new MouseMotionAdapter()
{
@Override
public void mouseDragged(MouseEvent e)
{
Graphics g = getGraphics();
x1 = e.getX();
y1 = e.getY();
g.drawOval(x1,y1,10,10);
}
});
}

public static void main(String [] args)
{
new MouseEventDemo();
}
}





windowEvent



EventFired byListener interfaceHandler method
java.awt.event.WindowEvent JDialog
JFrame
JWindow
WindowListenerwindowOpened()
windowClosing()
windowClosed()
windowIconified()
windowDeiconified()
windowActivated()
windowDeactivated()

Example of WindowEvent

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

class WindowEventDemo extends JFrame
{
int x1,y1,x2,y2;

WindowEventDemo()
{
setSize(300,300);
setVisible(true);
setTitle("WindowEvent Example");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}

public static void main(String[] args)
{
new WindowEventDemo();
}
}

No comments:

Post a Comment