o# # This combines the dynamic compilation of a Java class # so as to use an R object as an implementation of a Java interface # and also the dynamic callback to an R function. # library(SJava) .JavaInit() compiler <- .JavaConstructor("ForeignReferenceClassGenerator", "java.awt.event.ActionListener", "RButtonListener") dyn <- .Java("__Evaluator","dynamicClassLoader") .Java(dyn,"defineClass", compiler) btnCallback <- function(n = 0) { actionPerformed <- function(event) { n <<- n + 1 # print(event) print(n) } return(list(actionPerformed=actionPerformed, count =function() n)) } gui = function(n = 0) { b <- .JavaConstructor("javax.swing.JButton","Click me") f <- .JavaConstructor("GenericFrame", b, TRUE) cb <- btnCallback(n) # ref <- foreignReference(cb, "btnCB") ref <- foreignReference(cb) # XXX problems if .convert is not specified here as the default # causes problems. # We get into the isRReference in s_from_java_basic # and then we end up with the wrong thing. l <- .JavaConstructor("RButtonListener", ref, .convert = FALSE) print(l) print(class(l)) .Java(b,"addActionListener", l) invisible(list(button = b, callback = cb)) } # Then create two instances of the button setup # but with different initial counter values. # # gui() # gui(100)