Results 1 to 6 of 6

Thread: Creating a Custom Event in Java

  1. #1
    Join Date
    Jan 2009
    Posts
    65

    Creating a Custom Event in Java

    Hello, I am learning java programming where we need to create a custom event. I am not able to achieve it. I have tried it, but I am getting error while achieving it. If you know the details about it, then please provide me some programming code about it. So, it will helpful to me.

  2. #2
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Creating a Custom Event in Java

    Hello, you can able to create a custom attributes by the use of the code below. So, just make use of it, and get your problem solved:
    Code:
    public class testing : System.EventArgs 
    {   
    private string msg;
    public testing(string str) 
    {
    this.msg=str;
    }
    public string Message() 
    {
    return msg;
    }
    }

  3. #3
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Creating a Custom Event in Java

    Hello, if you make use of the code below then you will able to get the information about the custom event in java:
    Code:
    public class CustomClass
     {
        private int first;
        public delegate void ChangingHandler (object obj, CustomClassArgs customClassArg);
        public event ChangingHandler handler;
            
        public CustomClass(int n) 
    {
            this.first=n;
        }
        public void setOne(int p) 
    {
            this.first=p;
            if (p<5) 
    {
                CustomClassArgs customClassArg=new CustomClassArgs("Class is below, first value is : "+this.first);
                handler(this,customClassArg);
            }
        }
    
        public string strCheck() {
            return this.first.ToString();
        }
    }

  4. #4
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Creating a Custom Event in Java

    Hey, I have got the code below for creating the custom event in java:
    Code:
    EventHandlerClass eveHandler = new EventHandlerClass();
    Account account = new Account();
    account.SubscribeAccountBalanceLowEvent(
    new AccountBalanceEventHandler(eveHandler.HandleAccountLowEvent));
    account.SubscribeAccountBalanceLowEvent(
    new AccountBalanceEventHandler(eveHandler.HandleAccountLowEvent2));
    account.Withdraw(2.3M, 1);
    account.UnsubscribeAccountBalanceLowEvent(
    new AccountBalanceEventHandler(eveHandler.HandleAccountLowEvent2));
    account.Withdraw(2.3M, 2);
    account.UnsubscribeAccountBalanceLowEvent(
    new AccountBalanceEventHandler(eveHandler.HandleAccountLowEvent));
    account.Withdraw(2.3M, 3);

  5. #5
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Creating a Custom Event in Java

    For creating custom event in java you must need to make use of the code below. I have check it, I think you will able to get details from it.
    Code:
    import java.util.EventListener;
    import java.util.EventObject;
    import javax.swing.event.EventListenerList;
    class event extends EventObject 
    {
      public event(Object obj) {
        super(obj);
      }
    }
    interface eventListener extends EventListener 
    {
      public void myEventOccurred(event e);
    }
    class testing 
    {
      protected EventListenerList list = new EventListenerList();
      public void addeventListener(eventListener elistener) 
    {
        list.add(eventListener.class, elistener);
      }
      public void removeeventListener(eventListener elistener) 
    {
        list.remove(eventListener.class, elistener);
      }
      void fireevent(event e) {
        Object[] elisteners = list.getListenerList();
        for (int i = 0; i < elisteners.length; i = i+2) 
    {
          if (elisteners[i] == eventListener.class) 
    {
            ((eventListener) elisteners[i+1]).myEventOccurred(e);
          }
        }
      }
    }
    public class Main {
      public static void main(String[] argv) throws Exception 
    {
        testing c = new testing();
        c.addeventListener(new eventListener() 
    {
          public void myEventOccurred(event e) 
    {
            System.out.println("fired");
          }
        });
    
      }
    }

  6. #6
    Join Date
    May 2008
    Posts
    2,389

    Re: Creating a Custom Event in Java

    Hello, If you make use of the code below then you will able to get the custom event in java. So, just make use of it, and get your problem solved.
    Code:
    public class custEvent extends EventObject
    { 
    public custEvent(Object obj)
    { 
    super(obj); 
    } 
    } 
    public custEvent(Object obj) 
    {
     super(obj); 
    }
     }
    public interface custEventListener extends EventListener 
    {
     public void event(custEvent e);
     }
    public class MyClass 
    { 
    protected javax.swing.event.EventListenerList listenerList = new javax.swing.event.EventListenerList(); 
    public void addcustEventListener(custEventListener listener) 
    { 
    listenerList.add(custEventListener.class, listener); 
    } 
     void firecustEvent(custEvent e) 
    { 
    Object[] listeners = listenerList.getListenerList(); 
    for (int i=0; i<listeners.length; i+=2) 
    {
     if (listeners[i]==custEventListener.class) 
    {
     ((custEventListener)listeners[i+1]).event(e); 
    }
     } 
    } 
    }

Similar Threads

  1. Application for creating custom greeting cards
    By Rambha in forum Windows Software
    Replies: 5
    Last Post: 21-07-2011, 07:03 PM
  2. Creating custom Ubuntu iso
    By Rup_me in forum Operating Systems
    Replies: 5
    Last Post: 24-04-2010, 11:42 AM
  3. Creating Recovery Discs after custom install of Windows
    By AbrahamL in forum Operating Systems
    Replies: 4
    Last Post: 08-04-2010, 05:57 AM
  4. Creating a event log
    By Protector in forum Software Development
    Replies: 4
    Last Post: 18-01-2009, 08:16 PM
  5. Replies: 2
    Last Post: 30-03-2007, 11:46 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,035,592.76871 seconds with 16 queries