Drag-and-drop operations of javafx

Source: Internet
Author: User
Tags javafx tutorial

Disclaimer: the original articles of this blog are all personal originals and are copyrighted. Reprinted please indicate the source:
Http://blog.csdn.net/ml3947 (see a website directly copy without link, of course, my personal blog articles are currently transferred from my csdn blog ).


Hello everyone, the new javafx tutorial is coming again.

Is there a function for many programs to drag files into the program? Do you think drag-and-drop operations are cool? Of course, our javafx also supports this operation.

In javafx, we can not only drag content from one control to another, but also drag files from the system to the javafx control, you can even drag content from a javafx program to another javafx program.

Looks good, right? The drag and drop operations in javafx are described below.


First, make a small advertisement. My personal blog address:
Http://www.wjfxgame.com

Currently, my javafx examples can be run online on my blog (except for some ). Although the laptop data is lost, there are not many. However, I have been writing it myself (because I am developing the javafx game engine, it may not be very fast ).


Example of this tutorial: Click

Drag the drag me label to the drop on me label.

From the local area, drag an image to the label of drag a image to this to display the image you drag.

Drag a local text file to the label drag a TXT to this to display the content of the text you drag. (garbled characters may occur due to encoding issues, can use text encoded such as UTF-8 ).


:


In javafx, you can set these event listeners for drag-and-drop operations.

Setondragdetected (New eventhandler <mouseevent> ());

When you drag from a node, the drag operation is detected and the eventhandler is executed.


Setondragentered (New eventhandler <dragevent> ());

When you drag the widget to the target, this Event Callback is executed.


Setondragexited (New eventhandler <dragevent> ());

Execute this operation when you drag the widget to remove it from the target control.


Setondragover (New eventhandler <dragevent> ());

When you drag to the top of the target, it will not stop running.

 

Setondragdropped (New eventhandler <dragevent> ());

Execute this dragdropped event when you drag to the target and release the mouse.


Setondragdone (New eventhandler <dragevent> ());

When you drag and drop your hands, execute the drag to complete the operation.


Here, you must note that when you drag the widget to the target control, the dragenter command is executed first. Then, when you stay above the target control, the dragover operation is continuously specified.


Let's take a look at the code in the example:

m_drag.setOnDragDetected(new EventHandler<MouseEvent>() {@Overridepublic void handle(MouseEvent event) {Dragboard dragboard = m_drag.startDragAndDrop(TransferMode.ANY);ClipboardContent content = new ClipboardContent();content.putString(m_drag.getText());dragboard.setContent(content);}});m_drop.setOnDragEntered(new EventHandler<DragEvent>() {@Overridepublic void handle(DragEvent event) {m_drop.setTextFill(Color.RED);}});m_drop.setOnDragExited(new EventHandler<DragEvent>() {@Overridepublic void handle(DragEvent event) {m_drop.setTextFill(Color.BLACK);}});m_drop.setOnDragOver(new EventHandler<DragEvent>() {@Overridepublic void handle(DragEvent event) {if (event.getGestureSource() != m_drop && event.getDragboard().hasString()) {event.acceptTransferModes(TransferMode.COPY_OR_MOVE);}}});m_drop.setOnDragDropped(new EventHandler<DragEvent>() {@Overridepublic void handle(DragEvent event) {Dragboard dragboard = event.getDragboard();m_drop.setText(dragboard.getString());}});m_drag.setOnDragDone(new EventHandler<DragEvent>() {@Overridepublic void handle(DragEvent event) {m_drag.setText("");}});

M_drag is the drag me label. M_drop is the drop on me label.

When we detected the drag m_drag label, we used a dragboard, also known as "drag Board" (...). Of course, it cannot be called like this, but obviously, it is similar to the clipboard. We will use clipboardcontent to set the data to be transferred to the dragboard.

Others are relatively simple. When dragging to m_drop, the color of m_drop is changed.

During dragover, use event. accepttransfermodes (transfermode. Any); To set the type of data to be received.

When the drag is released, the dragboard data is obtained to set it to the label.


The above is an example of dragging data between controls in javafx. Of course, a lot of processing is not suitable, just to demonstrate all the drag methods, so every event is used.


Next let's take a look at the drag file:

m_imageView.setOnDragOver(new EventHandler<DragEvent>() {@Overridepublic void handle(DragEvent event) {if (event.getGestureSource() != m_imageView) {event.acceptTransferModes(TransferMode.ANY);}}});m_imageView.setOnDragDropped(new EventHandler<DragEvent>() {@Overridepublic void handle(DragEvent event) {Dragboard dragboard = event.getDragboard();List<File> files = dragboard.getFiles();if(files.size() > 0){try {m_imageView.setImage(new Image(new FileInputStream(files.get(0))));} catch (FileNotFoundException e) {e.printStackTrace();}}}});

There is only one dragover and dragdropped.

After the mouse is removed, the dragboard is also used to obtain the list of dragged files. Here, we use the first file to create an image as a data stream and set it to imageview.

The operation here is relatively simple. In fact, you can make more complicated judgments.


The text is the same:

 

m_textArea.setOnDragOver(new EventHandler<DragEvent>() {@Overridepublic void handle(DragEvent event) {if (event.getGestureSource() != m_imageView) {event.acceptTransferModes(TransferMode.ANY);}}});m_textArea.setOnDragDropped(new EventHandler<DragEvent>() {@Overridepublic void handle(DragEvent event) {Dragboard dragboard = event.getDragboard();List<File> files = dragboard.getFiles();if(files.size() > 0){m_textArea.setText(FileTools.readFile(files.get(0)));}}});

Isn't it easy? When the drag operation becomes popular in the javafx program, think about it. We can drag the javafx program to another javafx program, and we will feel very excited, there may be many unexpected applications (of course, the premise is that javafx can be recognized and widely spread ).


Considering that it is not convenient for you to copy and paste the source code, all your sample projects will be packaged and uploaded to the csdn download section without points.

Example project: http://download.csdn.net/detail/ml3947/5307918

Reprinted please indicate the source: http://blog.csdn.net/ml3947

Bytes -----------------------------------------------------------------------------------------

... After uploading the resource, it took a long time to display it. If the csdn download topic is too inefficient, it will be uploaded to other places.

Bytes ------------------------------------------------------------------------------------------

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.