On Tue, Aug 16, 2011 at 10:27 PM, Patrice Seyed apseyed@gmail.com wrote:
Right,
Hello abcl developers, Let's say I want to implement an anonymous inner class, just for example ActionListener with method actionPerformed: JButton button = new JButton("Enter"); button.addActionListener(new ActionListener (){ public void actionPerformed(ActionEvent e){
System.out.println("The Enter button was pressed"); } }); What is the best way to do this in abcl?
Hi Patrice,
Welcome to the ABCL list! "implementing an anonymous inner class" is ambiguous: when in Java you write an inner class, you do so to implement an interface or to extend another class. Inner classes are only a Java-the-language (javac) construct, but for the JVM they're classes like any others. So, your question should be: how do I implement an interface (like ActionListener) in ABCL? How do I extend a class (like MouseAdapter) in ABCL? For the first question, there are two operators: jmake-proxy and jinterface-implementation. The first is newer and more powerful but less documented. You can find both of them in the file java.lisp in ABCL's sources. For the simple case of interfaces with a single method (SAM - single abstract method) you can do like this (untested): (jmake-proxy "java.awt.event.ActionListener" (lambda (obj method event) (declare (ignore obj method)) (handle-my-event event)))
For the second question, there's currently no way to define new Java classes in Lisp. Technically it's doable and not particularly hard, but nobody did it yet. (ABCL used to support it long time ago). I was supposed to look into it some time ago but never found the actual time for doing it.
Hope that answers your question - if not, do not hesitate to write back!
Cheers, Alessio