Event Handling in Java

Put your problem here if it does not fit any of the other categories.

Event Handling in Java

Postby Grofit » Fri May 28, 2010 8:52 am

Hey,

This is from one of my other threads but thought i would keep that on topic and raise this seperatly, but im just trying to understand how event handling occurs in Java...

Im quite used to C# and in that you would do something like:

Code: Select all
public class SomeClass
{
   public event EventHandler OnComplete; // Or some other delegate instead of EventHandler

   public void SomeUpdateMethod()
   {
       if(OnComplete != null) { OnComplete(EventArg.Empty); }
   }
}

public class SomeOtherClass
{
   private SomeClass m_ClassInstance;
   
   public SomeOtherClass()
   {
      m_ClassInstance = new SomeClass();
      m_ClassInstance.OnComplete += Handler_OnComplete;
   }

   private void Handler_OnComplete(EventArgs e)
   {
      // Do some stuff when the event is fired
   }
}


However I cant seem to find any solid examples of this in Java, any code examples would be great. All im after currently is a way to notify a subscriber that an object has completed its job...
Grofit
Freshman
Freshman
 
Posts: 8
Joined: Thu May 27, 2010 9:40 pm

Top

Re: Event Handling in Java

Postby anarche » Fri May 28, 2010 12:38 pm

Java doesn't use delegates. You set a listener to an object for an event, such as

Code: Select all
View.setOnClickListener(myOnClickListener)


the easiest way to write the myOnClickListener is as a separate class (for learning) where myOnClickListener extends OnClickListener. Abstract methods in the api must be implemented (eg OnClick(View))

http://java.sun.com/docs/books/tutorial ... index.html

you can create anonymous classes, or nested classes for listeners - generally this is done, but is worse for reuse.

You can set multiple listeners per object, but can not be sure which order they will be fired in.
anarche
Master Developer
Master Developer
 
Posts: 369
Joined: Sun Apr 11, 2010 5:21 am

Re: Event Handling in Java

Postby Grofit » Fri May 28, 2010 7:12 pm

So am i right in saying it is impossible to create custom events in java, the best you can do is implement existing event handlers and hook them up to some custom code?
Grofit
Freshman
Freshman
 
Posts: 8
Joined: Thu May 27, 2010 9:40 pm

Re: Event Handling in Java

Postby pskink » Fri May 28, 2010 7:21 pm

Grofit wrote:So am i right in saying it is impossible to create custom events in java, the best you can do is implement existing event handlers and hook them up to some custom code?


describe "event"
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Re: Event Handling in Java

Postby Grofit » Fri May 28, 2010 7:45 pm

In my eyes an event is a way for one object to notify another object about something and sends over context data relating to that event.

Basically all i want to be able to do is make an event with a custom eventargs style object and then when that scenario occurs in the calling class it notifies the others. I tend to do alot of event based object relationships in C# and find it very flexible, whereas Java seems rather ridged in how you can notify other objects of changes...
Grofit
Freshman
Freshman
 
Posts: 8
Joined: Thu May 27, 2010 9:40 pm

Re: Event Handling in Java

Postby pskink » Fri May 28, 2010 7:56 pm

Grofit wrote:In my eyes an event is a way for one object to notify another object about something and sends over context data relating to that event.


so, yes, you can create custom "events"

take sources of e.g. android/view/View.java and see how they implemented OnClickListener

the same you can do with your custom listeners
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Top

Re: Event Handling in Java

Postby nicholas.hauschild » Sat May 29, 2010 2:24 am

Usually, if you want to create your own listener you would do something like this:

1. create an interface for your listener, defining the methods which will be used as callbacks when your specified event is triggered. (ie., OnClickListener with a method called onClick(...) with ... being the necessary parameters to tell what the status of the click was.

2. create an object that would want to listen for these methods. (ie., a Button class might want to listen for clicks, so you would allow it to have an OnClickListener, or rather, a list of OnClickListener's)

3. create a way to fire the onClick(...) method (ie., when your button receives a 'touch' or press, you would want to fire this onClick(...) method)

Good Luck!
-Nick
nicholas.hauschild
Master Developer
Master Developer
 
Posts: 310
Joined: Fri Dec 04, 2009 4:50 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot], Google Feedfetcher and 23 guests