0

In the application I'm developing in Java SE I use Luaj to implement functionality (this is a data collector application). The Java app reads a COM port of a device and gives the data to Lua event handlers which are written by the user of the application. Part of the user interface is also constructed from Lua, however, I'm having problems adding ActionListener objects (implemented in Lua as well) to Swing components, like JButton.

The code I'm currenty stuck at:

button = luajava.newInstance("javax.swing.JButton","test")
visuals:getPanel():add(button)

This creates a JButton object and puts it on a JPanel component. I'd like to define the action listener for this button in Lua as well.

Any idea how I can do that?

I tried the following, but it obviously does not work.

al = {}
function al.actionPerformed(ev)
  print("test")
end

button.addActionListener(al)
4

1 回答 1

1

我来的有点晚,但作为参考,swingapp.lua 脚本显示了如何处理监听器:

button:addActionListener(luajava.createProxy('java.awt.event.ActionListener',
{
  actionPerformed = function (e)
    print('Action', e)
  end,
}))

使用 Luaj-jse 3.0-alpha1 测试

于 2012-10-23T11:30:40.500 回答