How-To's > How do I add an object after a mouse click?


Use the insert before or insert after construction to add a new object to the scene content. Refer to the following code example, which produces this screen after two mouse clicks.

Screenshot of insert object application

import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.paint.*;
import javafx.scene.shape.*;
import javafx.scene.input.*;
import javafx.scene.image.*;

def image = Image { url: "{__DIR__}picture.png" }

var content: Node[] = [
   Rectangle{
       width: 400
       height: 400
       fill: Color.WHITE
       onMouseClicked: function( e: MouseEvent ):Void {
           insert ImageView {
               translateX: e.x
               translateY: e.y
               image: image
             } into content;
       }
   }

];

Stage {
   title: "Insert an object on mouse click"
   scene: Scene {
       content: bind content
   }
} 
  

Tips

  • Because the size of the scene is unspecified, it is set by the size of the rectangle, which becomes the clickable area.
  • The upper left corner of the rectangle is inserted at the click point.

Last Updated: November 2009
[Return to How-To's Home]



Rate This Article
Leave a Comment or Suggest a Topic

Do you have comments about this article? We welcome your participation in our community. Please keep your comments civil and on point. You may optionally provide your email address to be notified of replies—your information is not used for any other purpose. By submitting a comment, you agree to these Terms of Use.

 

English
日本語
한국어
简体中文
русский
Português do Brasil