adding a listener to a component added dynamically

21 views
Skip to first unread message

nPn

unread,
Nov 14, 2010, 11:33:36 PM11/14/10
to monkeybars-mvc
First of all, let me say that I kind of like this tool. I am really
just a hobbyist when it comes to programing. I like Java , but most
programing I do is in a language called SKILL which is lisp based, so
I like rubys lambdas and blocks. I learned a lot from looking at the
various documentation and the flickr_browser project. So my test app
is to have a gui that adds a variable set of (for now checkboxes with
image icons) to represent dominoes to a panel. Then I want to either
left or right click on the "domino" and have it "fall". So I add the
"dominoes" to a jpanel via ruby code rather than the a fixed form. I
can add a call such as

add_listener :type => :mouse, :components => [:panel]
after the set_view in the controller and then add a method
def mouse_clicked(event)
puts "detected_click"
p event.get_button
end
to the controller and it seems to work fine, but after I add the
"dominoes" via an other button, when I click on the checkbox icon
nothing happens.
it seems like add_listener is a class method? ... and while I can call
it later via MainController.add_listener blah, it does not seem to do
anything
Any idea's would be greatly appreciated

Thanks - John

nPn

unread,
Nov 15, 2010, 3:48:28 PM11/15/10
to monkeybars-mvc
So I found define_handler which seems to let me add a handler
dynamically, but the problem I think I have is that both add_listener
and define_handler want to find a variable in the java file. For
example I can attach a handler to a checkbox that I add statically
because the methods can find the component based on the java variable
name, but since I add the component via ruby the variable does not in
the java view, so it does not seem to find the component.

nPn

unread,
Nov 15, 2010, 6:27:11 PM11/15/10
to monkeybars-mvc
I found what I think is a nice solution to this problem

I did find the define_handler method and played around with that a
bit, but here is what I did

In the controller I have the
add_listener :type => :mouse, :components => [:panel]
and a method
def mouse_clicked(event)
puts "event source = #{event.source}"
end
to handle mouse clicks to the panel

So I have been trying to figure out how to get listeners on to the
components I add to the view dyamically

In the view I now have something like this
panel_mouse_listeners = panel.get_mouse_listeners #get the mouse
listeners for the panel
....
a_check_box = javax.swing.JCheckBox.new
panel_mouse_listeners.each do |listener|
a_check_box.add_mouse_listener listener
end

so I basically avoid the whole problem by just copying the mouse
listener references from the panel to the new components
Let me know if there is a better way, but this is good enough for my
test application
John



On Nov 14, 11:33 pm, nPn <npns...@gmail.com> wrote:

nPn

unread,
Nov 15, 2010, 6:33:33 PM11/15/10
to monkeybars-mvc
I found what I think is a nice solution to this problem

I did find the define_handler method and played around with that a
bit, but here is what I did

In the controller I have the
add_listener :type => :mouse, :components => [:panel]
and a method
def mouse_clicked(event)
puts "event source = #{event.source}"
end
to handle mouse clicks to the panel

So I have been trying to figure out how to get listeners on to the
components I add to the view dyamically

In the view I now have something like this
panel_mouse_listeners = panel.get_mouse_listeners #get the mouse
listeners for the panel
....
a_check_box = javax.swing.JCheckBox.new
panel_mouse_listeners.each do |listener|
a_check_box.add_mouse_listener listener
end

so I basically avoid the whole problem by just copying the mouse
listener references from the panel to the new components
Let me know if there is a better way, but this is good enough for my
test application
John

On Nov 14, 11:33 pm, nPn <npns...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages