Declares which components you want events to be generated for. add_listener takes a hash of the form :type => type, :components => [components for events] All AWT and Swing listener types are supported. See Monkeybars::Handlers for the full list.
The array of components should be strings or symbols with the exact naming of the component in the Java class declared in the view. As an example, if you have a JFrame with a text area named infoTextField that you wanted to receive key events for, perhaps to filter certain key input or to enable an auto-completion feature you could use:
add_listener :type => :key, :components => [:infoTextField]
To handle the event you would then need to implement a method named <component>_<event> which in this case would be info_text_field_key_pressed, info_text_field_key_released or info_text_field_key_typed.
If you have a single component you can omit the array and pass a single string or symbol.
add_listener :type => :key, :components => :infoTextField
You will run into errors if your component is a nested name, for example
add_listener :type => :document, :components => "infoTextField.document"
because when the event is generated and a handler is attempted to be located, the name infoTextField.document doesn’t map well to a method. To resolve this, the component name can be a hash, the key being the component name and the value being the desired callback name.
add_listener :type => :document, :components => {"infoTextField.document" => "info_text_field"}
This will cause the info_text_field_action_performed method to be called when the action performed event is generated by infoTextField.document.
If you want to add a listener to the view itself (JFrame, JDialog, etc.) then you can use :java_window as the component
add_listener :type => :window, :components => [:java_window]
If it is not possible to declare a method, or it is desirable to do so dynamically (even from outside the class), you can use the define_handler method.
If you wish to override the default event handling behavior, override handle_event
# File lib/monkeybars/event_handler_registration_and_dispatch_mixin.rb, line 62 def add_listener(details) handlers << details end
define_handler takes a component/event name and a block to be called when that event is generated for that component. This can be used in place of a method declaration for that component/event pair.
So, if you have declared:
add_listener :type => :action, :components => [:ok_button]
you could implement the handler using:
define_handler(:ok_button_action_performed) do |event| # handle the event here end
Note that handlers defined using this method will create implicit listener registrations the same as a declared method would.
define_handler also accepts multiple event names
define_handler(:ok_button_action_performed, :cancel_button_action_performed) do # handle event(s) here end
# File lib/monkeybars/event_handler_registration_and_dispatch_mixin.rb, line 88 def define_handler(*actions, &block) actions.each {|action| event_handler_procs[action.to_sym] << block} end
Generated with the Darkfish Rdoc Generator 2.