Event Handling in java

19,820 views
19,514 views

Published on

this ppt contain somewhat about Event Handling in java..
this ppt contain only 7 Event classes..

Published in: Education, Technology
7 Comments
11 Likes
Statistics
Notes
No Downloads
Views
Total views
19,820
On SlideShare
0
From Embeds
0
Number of Embeds
228
Actions
Shares
0
Downloads
1,046
Comments
7
Likes
11
Embeds 0
No embeds

No notes for slide

Event Handling in java

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

×