hxJava howto make a neat EventListener

72 views
Skip to first unread message

Adrian Veith

unread,
Feb 18, 2015, 11:07:51 AM2/18/15
to haxe...@googlegroups.com
Hi,

I am trying to find an easy way to make an EventListener for Java Classes. In Java there is something like this:

public interface ActionListener {
    
    public void actionPerformed(ActionEvent evt);
    
}
...
public void addActionListener(ActionListener l);


in order to pass an arbitrary event listener I do something like this:

class MyActionListener implements ActionListener {
  var _delegate: ActionEvent->Void;
  public inline function new(dg: ActionEvent->Void) _delegate=dg;

}

...

  obj.addActionListener(new MyActionListener(function(evt) { trace("hit me"); } ));


this works so far, but isn't very elegant and has the drawback, that for each type of event listener I need to define a wrapper class. Is there a better solution ?

cheers, Adrian.

Adrian Veith

unread,
Feb 18, 2015, 11:12:20 AM2/18/15
to haxe...@googlegroups.com
oops code should be:

class MyActionListener implements ActionListener {
  var _delegate: ActionEvent->Void;
  public inline function new(dg: ActionEvent->Void) _delegate=dg;
  public function actionPerformed(evt) _delegate(evt);


}...
public void addActionListener(ActionListener l);

Juraj Kirchheim

unread,
Feb 18, 2015, 7:47:44 PM2/18/15
to haxe...@googlegroups.com
Yes, there are many ways to tackle this problem. This might be one: https://github.com/andyli/hxAnonCls

If you can elaborate on your use case a bit further, we might even find more suited ones ;)

Best,
Juraj

--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.

Adrian Veith

unread,
Feb 19, 2015, 2:10:20 AM2/19/15
to haxe...@googlegroups.com
thanks for the link. 
I currently evaluate codename one (codenameone.com) which gives you a thin Layer over native UI on Android and iOS and others like Blackberry, Windows, Windows Phone. Code is written in Java (I use Haxe) and compiled to native on Android (Java of course) and iOS(Java to objc). What I tried works fine for Android, but I couldn't test on iOS, because I have problems with the sh... code signing.

cheers, Adrian.

Cauê Waneck

unread,
Feb 19, 2015, 8:29:26 AM2/19/15
to haxe...@googlegroups.com

I have an idea on how to handle this on the generator side. The only problem is that there's no annotation of what interface is expected to be an anonymous implementation versus what's expected to be an actal interface implementation.
The idea would involve transforming the single-method java interfaces on abstracts that can be cast from a Haxe function type - something like what already happens with delegates on C#.
The problem involves being able to make a clear differentiation between the interface and the abstract - since you can't for instance implement an abstract.

So it still needs some thought

Reply all
Reply to author
Forward
0 new messages