views:

185

answers:

1

I'm trying to extend a Java Swing component in Clojure, i.e. I want to extend a javax.swing.JComponent and add some custom methods implemented in pure Clojure in addition to all the standard inherited methods.

I've tried using "proxy" which works great if I just want a single instance (in the same way as an anonymous inner class). However I'd really like a named class so that I can generate an arbitrary number of instances.

What's the recommended way of doing this?

+6  A: 

Use gen-class (note that you can use it as an in-line function or in the namespace declaration).

(gen-class :extends javax.swing.JComponent ...)
Greg Harman
thanks Greg - however does this mean I will be forced to use AOT compilation? I'd quite like to be able to run this all at the REPL if possible.....
mikera
Yes, you'd have to use AOT compilation with this approach. If you need this purely in-REPL, perhaps you could just wrap the call to proxy in a function which returns the proxy object?
Greg Harman
Thanks Greg - the proxy method works very nicely. I was worried that successive calls would end up generating completely new anonymous class definitions but it appears that the compiler is clever enough to create a single class instance
mikera