This is the mail archive of the kawa@sourceware.org mailing list for the Kawa project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

easier Kawa object building with Swing example


For a while Kawa has supported:
  (MyClass keyword1: value1 ... kewordN: valueN)
as shorthand for:
  (let ((tmp (make MyClass)))
    (set! tmp:keyword1 value1)
    ...
    (set! tmp:keywordN valueN)
    tmp)
where
  (set! tmp:keywordI valueI)
could compile to either setting a field or calling a setKeywordI method.

Now, if there are extra non-keyword arguments, these get translated
to "add" calls.  These are useful for java.util Collections:
  (java.util.ArrayList 5 6 7)
They are also useful for java.awt.Container, including Swing's
JContainer, as shown in the example later.

Two more new features: First "SAM-conversion" is applied when a
lambda expression is provided in a context that requires a "SAM type",
which is an interface or abstract class with a single abstract method.
For example java.lang.Runnable or many XxxListener classes.
In that case an anonymous class is created.

Secondly, Kawa will also look for "addXxxYyy" methods if it sees
a keyword argument xxx-yyy:.

Putting it all together, we gt this demo:

(define-simple-class VBox (javax.swing.Box)
  ((*init*) (invoke-special javax.swing.Box (this) '*init* 1)))
(define-simple-class HBox (javax.swing.Box)
  ((*init*) (invoke-special javax.swing.Box (this) '*init* 0)))
(define-alias JFrame javax.swing.JFrame)
(define-alias JButton javax.swing.JButton)

(define value 0)

(define txt
  (javax.swing.JLabel
   text: "0"))

(define (set-value i)
  (set! value i)
  (set! txt:text (number->string i)))

(define fr
  (JFrame
     title: "Hello!"
     (VBox
      (javax.swing.Box:createGlue)
      txt
      (javax.swing.Box:createGlue)
      (HBox
       (JButton
	text: "Decrement"
	tool-tip-text: "decrement"
	action-listener: (lambda (e) (set-value (- value 1))))
       (javax.swing.Box:createGlue)
       (JButton
	text: "Increment"
	tool-tip-text: "increment"
	action-listener: (lambda (e) (set-value (+ value 1))))))))
(fr:setSize 200 100)
(set! fr:visible #t)
--
	--Per Bothner
per@bothner.com   http://per.bothner.com/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]