On 9/10/10 9:03 AM, Laurent PETIT wrote:
Got it. You cannot invoke (import) (or (ns)) from within the function
definition. The function is already being evaluated, and it's "too
late".

This works fine for me:

(do
   (import '(javax.swing JOptionPane))
   ((fn []
     (println "Hello World")
      (println (+ 2 2))
      (JOptionPane/showMessageDialog
        nil "Hello from the text panel" "Greeting"
        JOptionPane/INFORMATION_MESSAGE))))

So be aware that then some namespace will be changed by the call to
import (side effect on *ns*).

I would suggest either you create a temporary namespace for the code
to execute, either as Sean suggested, you write the call to
JOptionPane static methods and fields fully qualified:
(javax.swing.JOptionPane/showMessageDialog ...)

I see, those are good suggestions that help me better understand what is going on with namespaces. I can remember now in my work that I had other errors related to where I placed an import statement in the base code that I had resolved by pulling them out of a function (not really understanding then what worked, because at the same time I changed the form of it).

Strangely, when I click on "Update code" to evaluate that code it popped up the JOptionPane right away. I would think it would have not done that, because I was trying to return an anonymous function I could bind to the button action.

And then "Click Me!" to actually call the function produces an error.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at 
org.pointrel.guitest2$hookupActionPerformed$fn__54.invoke(guitest2.clj:13)
at org.pointrel.guitest2.proxy$java.lang.Object$ActionListener$46793e3a.actionPerformed(Unknown Source)
        at 
javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1882)

I'm presuming that (do ...) returns nil and so the nil gets bound instead of a function and that causes the error.

I just removed a set of parens that caused the anonymous fn to evaluate during the load-string call, and now it works the way I wanted to. I can't say the GUI design was very self-explanatory, so I can see how you might have thought success was different then I expected. :-)

This is the variant that works the way I wanted:

(do
  (import '(javax.swing JOptionPane))
  (fn []
     (println "Hello World")
     (println (+ 2 2))
     (JOptionPane/showMessageDialog
       nil "Hello from the text panel" "Greeting"
       JOptionPane/INFORMATION_MESSAGE)))


And this works also with the complete path as Sean suggested:

 (fn []
     (println "Hello World")
     (println (+ 2 2))
     (JOptionPane/showMessageDialog
       nil "Hello from the text panel2" "Greeting"
       javax.swing.JOptionPane/INFORMATION_MESSAGE))

So, my thanks go to you and Sean, for two different solutions. :-)

I'll need to look more into that namespace thing. I'm more used to Python/Jython recently where you can import stuff wherever you want (suboptimally if done in a frequently called function, of course). Obviously, I can see a functional language making different tradeoffs.

Anyway, I've just been noodling around in terms of what it would take to build an interactive GUI building system where you could easily change how some of the system worked at run-time as a sort of rapid prototyping (like Smalltalk systems usually can do). This amount of code seems to be the bare bones to doing that (at least as a proof-of-concept), so thanks again for the solutions. I know there are other Clojure GUI building stuff out there, already; I was just starting playing with something interesting to show the power and elegance of Clojure. :-)

--Paul Fernhout
http://www.pdfernhout.net/
====
The biggest challenge of the 21st century is the irony of technologies of abundance in the hands of those thinking in terms of scarcity.

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to