Method: JRubyFX::Controller::ClassMethods#on

Defined in:
lib/jrubyfx/controller.rb

#on(names, &block) ⇒ Object

call-seq:

on(callback, ...) { |event_info| block } => Method

Registers a function of name name for a FXML defined event with the body in the block. Note you can also just use normal methods

Examples

on :click do
  puts "button clicked"
end

on :moved, :pressed do |event|
  puts "Mouse Moved or Key Pressed"
  p event
end

Equivalent Java

@FXML
private void click(ActionEvent event) {
  System.out.println("button clicked");
}

@FXML
private void moved(MouseEvent event) {
  System.out.println("Mouse Moved or Key Pressed");
}

@FXML
private void keypress(KeyEvent event) {
  System.out.println("Key Pressed or Key Pressed");
}


214
215
216
217
218
219
220
221
# File 'lib/jrubyfx/controller.rb', line 214

def on(names, &block)
  [names].flatten.each do |name|
    class_eval do
      # must define this way so block executes in class scope, not static scope
      define_method name, block
    end
  end
end