Skip to content

Instantly share code, notes, and snippets.

@abhinayagarwal
Created March 12, 2014 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abhinayagarwal/9504509 to your computer and use it in GitHub Desktop.
Save abhinayagarwal/9504509 to your computer and use it in GitHub Desktop.
A simple application to show the feature of Clipboard and its usage with only Image
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.input.Clipboard;
import javafx.scene.input.DataFormat;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
/**
* A simple application to show the feature of Clipboard and its usage.
*
* Run the application, copy any image and double click on the scene would paste the image onto the scene
* The images will stack on each other !
*/
public class ClipboardImageCopying extends Application {
@Override
public void start(Stage stage) throws Exception {
final StackPane stack = new StackPane();
stack.setPrefSize(500, 500);
Scene scene = new Scene(stack);
stage.setScene(scene);
stage.show();
stack.addEventFilter(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>()
{
@Override
public void handle(MouseEvent mouseEvent) {
if (mouseEvent.getButton().equals(
MouseButton.PRIMARY)) {
if (mouseEvent.getClickCount() == 2) {
System.out.println("Double Click Triggered");
Clipboard clipboard = Clipboard.getSystemClipboard();
if(clipboard.hasContent(DataFormat.IMAGE))
{
final ImageView image = new ImageView();
image.setImage(clipboard.getImage());
AnchorPane anchorPane = new AnchorPane();
AnchorPane.setLeftAnchor(image, mouseEvent.getX());
AnchorPane.setTopAnchor(image, mouseEvent.getY());
anchorPane.getChildren().addAll(image);
stack.getChildren().add(anchorPane);
}
}
}
}
});
}
public static void main(String args[])
{
launch(args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment