Unable to connect Javafx controller event with FXML shape. (Java) -


i'm having trouble understanding why super, super simple method isn't working. i've got standard mvc setup - have controller controls layout (the fxml file). in fxml, i've created various shapes see if issue shapes in particular (it wasn't...). want attach method in controller shape in fxml causes counter increase 1 per click of shape, , outputs current count console. it's not working!

here's relevant code:
(controller class)

@fxml sphere asteroid; private int counter = 0;  @fxml void addrandomresource(actionevent event) {     asteroid.setonmousepressed(new eventhandler<mouseevent>(){         public void handle(mouseevent event){             counter += 1;             system.out.println(counter);         }     }); }   

(fxml)

<sphere fx:id="asteroid" layoutx="250.0" layouty="300.0" radius="125.0" />   

i know controller connected because other buttons on page work... that's not issue. , can't add "onaction" property shape, should able use fx:id recognize it, right?
appreciated, thanks!

what doing wrong

  1. you using wrong event type.
  2. you not referencing event handler fxml.
  3. you should not try register event handler in event handler case this.

mouse clicks generate mouse_clicked mouseevents serviced onmouseclicked event handlers. actionevents other purposes, such button firings.

how fix program handle click events

in fxml need reference controller's click event handler:

<sphere fx:id="asteroid" onmouseclicked="#addrandomresource" layoutx="250.0" layouty="300.0" radius="125.0" / 

in controller need use correct signature accept event:

@fxml void addrandomresource(mouseevent event) {     counter += 1;     system.out.println(counter); }   

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -