How-To's > How do I add an object after a mouse click?
Version: JavaFX 1.3 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: April 2010
[Return to How-To's Home]