Method of realizing mouse drag and drop function in Java
This paper describes the method of Java using mouse drag and drop to exchange program data, that is, the so-called mouse drag and drop function. The drag and drop function of the mouse is very common in graphical systems. Java provides Java awt. DND and Java awt. Datatransfer package to support this function. This example demonstrates how to implement drag and drop in the program when "Hello world!" in the upper part of the window Click the label and drag it to the text box at the bottom of the window. Release it, and "Hello world!" will be added to the text box Text; If you continue the above process, you will continue to add the text.
The specific implementation ideas and methods of the program function are as follows: in the implementation of mouse drag and drop, the two most important concepts are drag source and drop target, namely drag source and drop target. Both drag source and drop target are associated with visual components (how to drag if they are not visible?). The essence of drag and drop technology is to transfer the data on the drag source component to the drop target component. Therefore, from the bottom, drag and drop is very similar to the clipboard technology in the above example.
Implementation of drag source: drag source class must first create a draggesturerecognizer instance, indicating that this class is a drag source component class or contains drag source components. This can be achieved by calling the createdefaultdraggesturerecognizer() method of the datasource object. The specific implementation is as follows:
The above statement indicates that the drag source component is the instance object of the class itself, and the type of drag and drop to be completed is dndconstants ACTION_ COPY_ OR_ The class of move type that implements the draggesturelistener interface is this class. Drag sources generally implement the draggesturelistener interface, which defines a draggesturerecognized() method. When dragging starts, draggesturelistener listens to events, and then turns to the draggesturerecognized() method to process events, such as sending the data of the drag source. Specific code:
The drag source must also implement the dragsourcelistener interface, which defines the event processing methods of drag related states. Such as dragenter, dragover, dropactionchanged, dragexit and other methods. In this example, the dragenter () method will be implemented to set the cursor shape during dragging, and other methods are empty. The specific implementation code is as follows:
Implementation of placement target: a dragtarget instance must be created in the placement target class to indicate that this class is a placement target component class or contains placement target components. The implementation is as follows:
The above statement indicates that the placement target is this Jtextfield1 object, the drag and drop operation is dndconstants ACTION_ COPY_ OR_ Move type, and the class that implements the droptargetlistener interface is this class. Corresponding to drafsourcelistener, the drop target or its class generally implements the droptargetlistener interface. The interface also defines many methods, such as dragenter and dragover, to handle events when the drag and drop process enters different stages. This example only cares about the drop () method, that is, the event processing when the mouse is released on the target component. It is generally used to process the data passed to. For example, in this example, the text data passed will be displayed on the jtextfield component, and other methods are empty. The specific code is as follows:
Program code:
1. Create a new project named jdraganddropdemo. 2. Create a new application named jdraganddropdemo; The main window is called mainframe, and the title is jdraganddropdemo. 3. Create a new class named dragjlabel and inherit the jlabel class. 4. Use wizards|implements interface to make dragjlabel class implement draggesturelistener and dragsourcelistener interfaces. 5. Add a new attribute dragsource DS in the class dragjlabel. The code is as follows:
6. Write the construction method of dragjlabel class.
7. Implement the draggesturerecognized() method in the dragjlabel class, wrap and send data.
8. Implement the dragenter () method in the dragjlabel class and set the shape of the cursor.
9. Add a jtextfield component in the lower part of the design window of the mainframe class, and create a dragjlabel instance in the class. The specific code is as follows:
10. Write the initialization method jbinit () of the mainframe class, set the initial properties of the component, and create a new droptarget instance. The code is as follows:
11. Use the wizards|implements interface to make the mainframe class implement the droptargetlistener interface.
12. Implement the method drop () inherited from the droptargetlistener interface to process the passed data. The specific code is as follows: