Jess> (defglobal ?*b* = (new java.awt.Button "Hello"))What should not be obvious is how, from Jess, you can arrange to have something happen when the button is pressed. For this, I have provided a full set of EventListener classes:
An example should clarify matters. Let's say that when the Hello button is pressed, you would like the string Hello, World! to be printed to standard output (how original!). What you need to do is:
Jess> ;; Create the widgets (defglobal ?*f* = (new java.awt.Frame "Button Demo")) Jess> (defglobal ?*b* = (new java.awt.Button "Hello")) Jess> ;; Define the deffunction (deffunction say-hello "Unconditionally print a message" (?evt) (printout t "Hello, World!" crlf)) Jess> ;; Connect the deffunction to the button (?*b* addActionListener (new jess.awt.ActionListener say-hello (engine))) Jess> ;; Assemble and display the GUI (?*f* add ?*b*) Jess> (?*f* pack) Jess> (set ?*f* visible TRUE)The Jess engine function returns the jess.Rete object in which it is executed, as an external address. You'll have to quit this program using ^C. To fix this, you can add a WindowListener which handles WINDOW_CLOSING events to the above program:
Jess> ;; If the event is a WINDOW_CLOSING event, exit the program (deffunction frame-handler (?evt) (if (= (?evt getID) (get-member ?evt WINDOW_CLOSING)) then (call (get ?evt source) dispose) (exit))) Jess> ;; Connect this deffunction to the frame (?*f* addWindowListener (new jess.awt.WindowListener frame-handler (engine)))Now when you close the window Jess will exit. Notice how we can examine the ?evt parameter for event information.
We have used the "raw" AWT widgets here, but this same technique works fine with Swing as well (the new GUI toolkit for Java 1.2).
Jess> (defglobal ?*f* = (new javax.swing.JFrame "Button Demo")) Jess> (defglobal ?*b* = (new javax.swing.JButton "Hello")) Jess> (defglobal ?*p* = (get ?*f* "contentPane")) Jess> (deffunction say-hello (?evt) (printout t "Hello, World!" crlf)) Jess> (call ?*b* addActionListener (new jess.awt.ActionListener say-hello (engine))) Jess> (call ?*p* add ?*b*) Jess> (call ?*f* pack) Jess> (set ?*f* visible TRUE) Jess> (deffunction frame-handler (?evt) (if (= (?evt getID) (get-member ?evt WINDOW_CLOSING)) then (call (get ?evt source) dispose) (exit))) Jess> (?*f* addWindowListener (new jess.awt.WindowListener frame-handler (engine)))
See the demo examples/frame.clp for a slightly more complex example of how you can build an entire Java graphical interface from within Jess.
Jess> ;; A painting deffunction. This function draws a red 'X' between the ;; four corners of the Canvas on a blue field. (deffunction painter (?canvas ?graph) (bind ?x (get-member (call ?canvas getSize) width)) (bind ?y (get-member (call ?canvas getSize) height)) (?graph setColor (get-member java.awt.Color blue)) (?graph fillRect 0 0 ?x ?y) (?graph setColor (get-member java.awt.Color red)) (?graph drawLine 0 0 ?x ?y) (?graph drawLine ?x 0 0 ?y)) Jess> ;; Create a canvas and install the paint routine. (bind ?c (new jess.awt.Canvas painter (engine)))A simple but complete program built on this example is in the file examples/draw.clp in the Jess distribution.