I am trying to build a Java GUI using ABCL. However, it throws an error.
#<THREAD "interpreter" {67D71B82}>: Debugger invoked on condition of type JAVA-EXCEPTION
Java exception 'java.lang.NoSuchMethodException: No applicable method named add found in javax.swing.JPanel'.
Restarts:
0: ABORT Return to debug level 1.
1: TOP-LEVEL Return to top level.
My code is as follows:
(defconstant +jframe+ "javax.swing.JFrame")
(defconstant +jpanel+ "javax.swing.JPanel")
(defconstant +button+ "javax.swing.JButton")
(defconstant +flowLayout+ "java.awt.FlowLayout")
(defconstant +dimension+ "java.awt.Dimension")
(defun make-frame (name width height)
(let ((this (jnew +jframe+ name))
(dims (jnew +dimension+ width height)))
(jcall "setPreferredSize" this dims)
this))
(defun make-panel ()
(let ((this (jnew +jpanel+)))
this))
(defun make-button (name)
(let ((this (jnew +button+ name))
this)))
(defun main ()
(let ((frame (make-frame
"This is my frame"
400 300))
(panel (make-panel))
(button1 (make-button
"Press me"))
)
(jcall "add" frame panel)
(jcall "add" panel button1 (jfield +flowLayout+ "LEFT"))
(jcall "pack" frame)
(jcall "setVisible" frame t)
))
The error is on line (jcall "add" panel button1 (jfield +flowLayout+ "LEFT"))
. Commenting this out leads to a working application.
According to [1] you need to use
jclass
to get the java class objects for each of the parameters, and thenjmethod
to find the right overload, and then finally you can calljcall
1: https://armedbear.common-lisp.dev/doc/abcl-user.html