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.

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]
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 repliesyour information is not used for any other purpose. By submitting a comment, you agree to these Terms of Use.