2. Some common term used in EventHandling Event : An event is an Object that describes a state change in a Source. * Some of the activities that causes event to be generated are : *Pressing a Button. *Entering a character through Key Board. *Selecting an item form a list etc.
3. Some Common term Conti.. Event Source : A source is an object that generates an event. Some general Event Sources are: #Button, CheckBox #List, MenusItem #Window, TextItems Etc… Here is a general form for adding a listener to an event Source : public void addTypeListener(TypeEvent e) * Type is the name of the event. * e is the reference of the event listener.
4. Some common terms Conti… Event Listener : A Listener is an object that is notified when an event occurs. For example : MouseMotionListener interface define two events: *When mouse is dragged. * When mouse is moved.
5. Some common terms Conti… For implementing event listener we have to import the following Statement: import java.awt.event.*;
6. Event Handling There are a number of event class provided by java : But we’ll discuss today only 7 classes, namely: *ActionEvent *KeyEvent *MouseEvent *MouseMotionEvent *FocusEvent *WindowEvent *ItemEvent
7. Action Event Class Action listeners are probably the easiest — and most common — event handlers to implement. To write an Action Listener, follow the steps given below: Declare an event handler class and specify that the class either implements an ActionListener interface or extends a class that implements an ActionListener interface. For example: public class MyClass implements ActionListener { }
8. Actionevent Listener conti….. Register an instance of the event handler class as a listener on one or more components. For example: someComponent.addActionListener(instanceOfMyClass); Include code that implements the methods in listener interface. For example: public void actionPerformed(ActionEvent e) { if(e.getSource()==someComponent) //code that reacts to the action... }
9. Example of Action Listener public class changePanel implements ActionListener{ //Initializing jf,jb,jl1,jl2,jp1,jp2… changePanel(){ jf=new JFrame("Presentation"); jb=new JButton("click"); jl1=new JLabel("1st PANEL"); jl2=new JLabel("2nd PANEL"); jp1=new JPanel(); jp2=new JPanel(); jf.setSize(500,500); jf.setBackground(Color.LIGHT_GRAY); jf.setLayout(new FlowLayout()); jf.add(jb); jp1.setSize(200,200); jf.add(jp1); jp1.add(jl1); jp2.add(jl2); jf.setVisible(true);
13. Key Event Class This class has 3 methods in Listener interface Namely: public void keyTyped(KeyEventae) {//active on typing a code…..} public void keyPressed(KeyEventae) {//active on pressing a key……} public void keyReleased(KeyEventae) {//active on realesing a key…..}
14. Example….. Package presentation; import java.awt.*; Import java.awt.event.*; Public class keyEventDemo implements KeyListeners{ //initialization occur of jf,jlabel,jText; //memory allocation to them jf.setSize(500,500); jf.setBackground(Color.LIGHT_GRAY);
15. Conti…. jf.setLayout(null); jt1.setBounds(400,200,180,30); jf.add(jt1); jl1.setBounds(100,200,200,30); jf.add(jl jt1.addKeyListener(this); jf.setVisible(true); } public void keyTyped(KeyEvent e) { jl1.setText("key is typed"); }
16. Conti… public void keyPressed(KeyEvent e) { jl1.setText("key is pressed"); } public void keyReleased(KeyEvent e) { jl1.setText("key is relesed"); } }
17. Output of the example is..No interface method is active yet
21. Mouse Event Class This class has 5 interface method as follows : public void mouseClicked(MouseEvent e) {…} Called just after the user clicks the listened-to component. public void mousePressed(MouseEvent e) {….} Called just after the user presses a mouse button while the cursor is over the listened-to component. public void mouseReleased(MouseEvent e) {...} Called just after the user releases a mouse button after a mouse press over the listened-to component public void mouseEntered(MouseEvent e) {…….} Called just after the cursor enters the bounds of the listened-to component. public void mouseExited(MouseEvent e) {……..} Called just after the cursor exits the bounds of the listened-to component.
22. Example of Mouse Listener.. package presentation; Import java.awt.*; Import java.awt.event.*; public class changePanel implements MouseListener{ //initialization of varibles occur //memory is allocated
23. jf.setSize(500,500); jf.setBackground(Color.LIGHT_GRAY); jf.setLayout(null); jt1.setBounds(300,150,180,30); jf.add(jt1); jl1.setBounds(100,150,200,30); jf.add(jl1); jf.setVisible(true); jt1.addMouseListener(this); } public void mouseClicked(MouseEvent e) { jl1.setText("Mouse is clicked"); } public void mousePressed(MouseEvent e) { jl1.setText("Mouse is Pressed"); }
24. Example conti….. public void mouseReleased(MouseEvent e) { jl1.setText("Mouse is Released"); } public void mouseEntered(MouseEvent e) { jl1.setText("Mouse is Released"); } public void mouseExited(MouseEvent e) { jl1.setText("Mouse is Exited"); } }
25. Output of the example..When no interface method is called..
26. Output of the example …when mouse is entered in the text field…..
27. Output of the example …when mouse is Exited in the text field…..
28. Output of the example …when mouse is Clicked in the text field…..
29. Output of the example …when mouse is Pressed in the text field…..
30. Output of the example …when mouse is Released in the text field…..
31. MouseMotionEvent Class This class provides 2 interface methods: *mouseDragged Method: executed when mouse is dragged over the listened-to component.. *mouseMoved Method: executed when mouse is moved over the listened-to component…
32. Example …. Package presentation; import java.awt.*; Import java.awt.event.*; Public class mouseMotionDemo implements MouseMotionListeners{ //initialization occur of jf,jlabel,jText; //memory allocation to them jf.setSize(500,500); jf.setBackground(Color.LIGHT_GRAY);
33. jf.setLayout(null); jt1.setBounds(300,150,180,30); jf.add(jt1); jl1.setBounds(100,150,200,30); jf.add(jl1); jf.setVisible(true); jt1.addMouseMotionListener(this); } public void mouseDragged(MouseEvent e) { jl1.setText("Mouse is dragged"); } public void mouseMoved(MouseEvent e) { jl1.setText(“ Mouse is Moved"); }}
36. FocusEvent Listener Class This class provide two interface methods: focusGained: Called just after the listened-to component gets the focus. focusLost: Called just after the listened-to component Loses the focus.
37. Example package presentation5; import java.awt.*; import java.awt.event.*; public class FocusListenertest extends Frame implements FocusListener { //initialization occur… public FocusListenertest() { //Memory allocation add(b1=new Button ("First"),"South"); add(b2=new Button ("Second"),"North"); add(l); b1.addFocusListener(this); b2.addFocusListener(this); setSize(200,200);
38. Example conti… } public void focusGained(FocusEventfe) { if(fe.getSource()==b1) { l.setText("focus gained of first & Lost of second"); } public void focusLost(FocusEventfe) { if(fe.getSource()==b1) { l.setText("focus Lost of First & Gained of Second "); }
39. Output of the Example..When Button First is pressed…
41. WindowEvent Class This class provide 8 interface methods: # windowOpened: Called just after the listened-to window has been shown for the first time. #windowClosing: Called in response to a user request for the listened-to window to be closed. To actually close the window, the listener should invoke the window's dispose or setVisible(false) method.
42. WindowEvent Class conti… windowClosed: Called just after the listened-to window has closed. windowIconified: Called just after the listened-to window is iconified . windowDeicoified: Called just after the listened-to window is deiconified.
43. WindowEvent Class conti… windowActivated and windowDeactivated : Called just after the listened-to window is activated or deactivated, respectively. These methods are not sent to windows that are not frames or dialogs. For this reason, we prefer the 1.4 windowGainedFocus andwindowLostFocus methods to determine when a window gains or loses the focus.
44. ItemEvent Class This class provide one interface method: itemStateChanged: Called just after a state change in the listened-to component.