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 ...) HTH, -- Laurent 2010/9/10 Paul D. Fernhout <pdfernh...@kurtz-fernhout.com>: > I'm just trying out Clojure a bit, so this may be an obvious simple thing > I'm misunderstanding. > > This code in the test file below allows editing a Clojure function that is > updated when an "Update Code" button is pressed, with the new value > connected to a GUI button "Click Me!" as a proxy ActionListener. > > It starts with a default function that has not been dynamically loaded from > a string that pops up a JOptionPane that displays the text in the text pane. > > Evaluating simple code like "(fn [] (println \"Hello World\"))" > with no import dependencies works. > > What am I doing wrong to not have the evaluated code have access to > JOptionPane? > > I get: > > user=> Exception in thread "AWT-EventQueue-0" java.lang.Exception: No such > namespace: JOptionPane (NO_SOURCE_FILE:6) > > when I try the "Update Code" button. > > The initial text includes an import statement. > > What do I misunderstand that the import does not seem to work? > > Anyway, I'm new to Clojure, so I don't really get the import or ns command > that well yet. Any suggestions would be appreciated. Feel free to treat this > code example as if it were in the public domain. > > =========== guitest2.clj > > (ns org.pointrel.guitest2 > ;(:require ) > ;(:use ) > ;(:import ) > ) > > (import > '(javax.swing JFrame JPanel JButton JOptionPane JTextPane) > '(java.awt.event ActionListener)) > > (defn hookupActionPerformed [receiver function] > (println "updating hook" receiver function) > (let [act (proxy [ActionListener] [] (actionPerformed [event] (function)))] > (.addActionListener receiver act) > ) > ) > > (def initial-text "(fn [] > (println \"Hello World\") > (println (+ 2 2)) > ;(ns org.pointrel.guitest2) > (import '(javax.swing JOptionPane)) > (JOptionPane/showMessageDialog > nil \"Hello from the text panel\" \"Greeting\" > JOptionPane/INFORMATION_MESSAGE) > )" > ) > > (defn window [] > (def frame (JFrame. "Hello Frame")) > > (def panel (JPanel.)) > (.setContentPane frame panel) > > (def test-button (JButton. "Click Me!")) > (.add panel test-button) > > (def update-button (JButton. "Update code")) > (.add panel update-button) > > (def text (JTextPane.)) > (.setText text initial-text) > (.add panel text) > > (defn say-hello [] > (let [text-contents (.getText text)] > (JOptionPane/showMessageDialog > nil text-contents "Greeting" > JOptionPane/INFORMATION_MESSAGE))) > > (hookupActionPerformed test-button say-hello) > > (defn update-code [] > (let [text-contents (.getText text) > discard (printf text-contents) > user-function (load-string text-contents)] > (println "update-code") > (.removeActionListener test-button (aget (.getListeners test-button > ActionListener) 0)) > (hookupActionPerformed test-button user-function) > )) > > (hookupActionPerformed update-button update-code) > > (.setSize frame 200 200) > (.setVisible frame true) > ) > > (window) > > ; (eval (read-string "(println \"Hello World\")")) > ; (load-string "(fn [] (println \"Hello World\"))") > ; (eval ((fn [] (println "Hello World2")))) > > =========== > > There's probably stylistic issue, too. :-) > > By the way, the println calls don't seem to show up in NetBeans 6.9.1 with > Enclojure after the window opens, and when I try to use code that generates > an error I get "Repl is disconnected" that I don't know how to recover from > without restarting NetBeans, so I am testing this with: > > $ java -cp jline-0.9.94.jar:clojure.jar jline.ConsoleRunner clojure.main > Clojure 1.2.0 > user=> (load-file "guitest2.clj") > > --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 -- 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