<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    sunfruit[請(qǐng)?jiān)L問(wèn)http://www.fruitres.cn]

    --我相信JAVA能走得更遠(yuǎn) QQ:316228067

    [JAVA]用戶自定義事件監(jiān)聽(tīng)

        --sunfruit

        用戶自定義事件

       

    User defined Event listeners java

    All of us who have used java.awt.event package in either applets
    or in Frames or in swing JFrame, shall agree to the fact that event
    propagation and listener concept is very very useful for capturing
    any action initiated either by user or by system, should be caught
    at the right place and without unneccessarily wasting time traversing
    through all the way from begining to end of source code just to know
    any takers for this event.



    This concept is implemented in form of MouseEvent, KeyEvent, WindowEvent so on so forth.

    JDK has given us a handle for creating our own event object and as well
    as event handling framework using java.util.EventObject and java.util.EventListener.

    You can imagine a scenario whereby you have to write an application, in
    which a email notification feature in a forum.

    Whenever a special event takes place , then your have to notify all the users
    who have registered themselves for this program.

    One solution is to use a Database and query all the users from the table
    and send notification to each and every user.
    But this is more of Database centric approach like old days thick client
    and server model.

    Think about the overhead on database and instead another solution is
    to create an event notification framework, whereby you have to create
    your own version of Event Object and Event Listeners.

    Event object represents the special event and all the users who register
    themselves for the event implements this Event listener.

    Let us take a more deeper plunge into this:

    1. Create a Demo event by creating a DemoEvent.java file that
    extends to java.util.EventObject.

    This class has a constructor that takes the event originator object (DemoSource).
    and a method getSource(), that gives the reference to the object that originated
    this event.

    DemoEvent.java
    
    package demo.listener;
    
    import java.util.EventObject;
    
    public class DemoEvent extends EventObject
    {
    	Object obj;
    	public DemoEvent(Object source)
    	{
    		super(source);
    		obj = source;
    
    	}
    	public Object getSource()
    	{
    		return obj;
    	}
    	public void say()
    	{
    		System.out.println("This is say method...");
    	}
    
    }
    
    


    2. Create the source of event notification file DemoSource.java
    It has a java.util.Vector that acts like a repository for storing all the
    listeners to the events this source provides.

    It has two other common method such as addListener(..) is to add any
    new user to this event.

    And notifyDemoEvent(..) is to alert all the users about this perticular event
    occurance.

    DemoEvent.java
    
    package demo.listener;
    import java.util.*;
    
    public class DemoSource
    {
    	private Vector repository = new Vector();
    	DemoListener dl;
    	public DemoSource()
    	{
    
    	}
    	public void addDemoListener(DemoListener dl)
    	{
    		repository.addElement(dl);
    	}
    	public void notifyDemoEvent()
    	{
    		Enumeration enum = repository.elements();
    		while(enum.hasMoreElements())
    		{
    			dl = (DemoListener)enum.nextElement();
    			dl.demoEvent(new DemoEvent(this));
    		}
    	}
    }
    
    


    3. Define a listener interface that extends to java.util.EventListener
    and contains the method for invoking the Event, here it is demoEvent(DemoEvent de).

    DemoListener.java
    
    package demo.listener;
    
    import java.util.EventListener;
    
    public interface DemoListener extends EventListener
    {
    	public void demoEvent(DemoEvent dm);
    
    }
    
    



    4. Define all the users, who want to listen to this events.
    These objects should implement DemoListener, to be able to
    capture the event propagated from DemoSource.java.

    Listener1.java
    
    package demo.listener;
    
    public class Listener1 implements DemoListener
    {
    	public void demoEvent(DemoEvent de)
    	{
    		System.out.println("Inside listener1...");
    	}
    }
    
    


    Listener2.java
    
    package demo.listener;
    
    public class Listener2 implements DemoListener
    {
    	public void demoEvent(DemoEvent de)
    	{
    		System.out.println("Inside listener2...");
    	}
    }
    
    

    Listener3.java
    
    package demo.listener;
    
    public class Listener3 implements DemoListener
    {
    	public void demoEvent(DemoEvent de)
    	{
    		System.out.println("Inside listener3...");
    	}
    }
    
    


    5. Now it time to write the client for testing this Framework.
    Create a client such as TestDemo.java and create a DemoSource
    object and all the users such as Listener1, Listener2, Listener3.
    And add all these listeners to the demo source by using the addDemoListener(..).
    When the notifyDemoEvent(..) is called on the demo source object,
    the event gets notified to all the users (listener1, listener2, listener3)
    and  the  output comes out to be

    Inside listener1...
    Inside listener2...
    Inside listener3...


    TestDemo.java
    
    package demo.listener;
    
    public class TestDemo
    {
    	DemoSource ds;
    
    	public TestDemo()
    	{
    		try{
    			ds = new DemoSource();
    			Listener1 l1 = new Listener1();
    			Listener2 l2 = new Listener2();
    			Listener3 l3 = new Listener3();
    
    			ds.addDemoListener(l1);
    			ds.addDemoListener(l2);
    			ds.addDemoListener(l3);
    
    			ds.notifyDemoEvent();
    
    		}catch(Exception ex){ex.printStackTrace();}
    	}
    
    	public static void main(String args[])
    	{
    		new TestDemo();
    	}
    }
    

    posted on 2006-02-19 17:58 sunfruit 閱讀(3538) 評(píng)論(0)  編輯  收藏 所屬分類: JAVA SE & EE

    主站蜘蛛池模板: 免费成人激情视频| 亚洲美女在线国产| 黄网站色视频免费观看45分钟| 国产又大又粗又硬又长免费| 日韩精品免费一线在线观看 | 国产女高清在线看免费观看| 一级做a爰全过程免费视频毛片| 久久精品亚洲中文字幕无码网站 | 久久久久久一品道精品免费看 | 18禁在线无遮挡免费观看网站| 亚洲国产精品综合福利专区| 免费A级毛片无码A∨男男| 国产一精品一av一免费爽爽| 国产成+人+综合+亚洲专| 亚洲性日韩精品一区二区三区| 日本一卡精品视频免费 | 国产精品免费久久久久久久久| 91亚洲国产在人线播放午夜 | 国产AⅤ无码专区亚洲AV| 成人在线免费看片| 黄色一级免费网站| 亚洲香蕉在线观看| 亚洲gv白嫩小受在线观看| 永久中文字幕免费视频网站| 免费无码毛片一区二区APP| 国产精品亚洲一区二区三区在线观看| 亚洲av最新在线网址| 国产人妖ts在线观看免费视频| 国产精品免费无遮挡无码永久视频 | 国产精品青草视频免费播放| 亚洲综合久久一本伊伊区| 亚洲国产精品成人精品无码区 | 日本三级2019在线观看免费| 久久嫩草影院免费看夜色| 亚洲综合欧美色五月俺也去| 亚洲av无码国产精品色午夜字幕| 婷婷综合缴情亚洲狠狠尤物| 在人线av无码免费高潮喷水| 无码国产精品一区二区免费模式| v片免费在线观看| 亚洲AV网一区二区三区|