Atitit. Comparison and implementation of d & D drag & drop drag Function c #. net java swing,
Atitit. Comparison and implementation of d & D drag & drop drag Function c #. net java swing
1. To implement a D & D operation, there are generally three steps: 1
2.. net blackhead dragging mechanism. A DragEnter event must exist (write the DragDrop event separately without the drag function). 2
3. --- java blackhead dragging .. there must be a DragEnter event (writing a Drop event alone will not have the drag function) 2
4. Code 3
5. Refer to 5
1. To implement a D & D operation, there are generally three steps:
First, implement a drag source, which is associated with the corresponding components.
Step 2: implement a drag-and-drop target, which is used to receive drag-and-drop objects.
Step 3: implement a data transmission object that encapsulates the dragged data
__________________________________________
|
| DragSource Component | DropTarget Component |
| _____________________ | ____________________ |
|
| ____________ Transferable Data _________________ |
The Transferable interface ensures that DropTarget Component can read the information contained in the dragged object.
If drag is implemented in the same virtual machine, DragSource Component will pass a reference to DropTarget Component
However, if data is transmitted between different JVMs or between the JVM and the local system, we must implement a Transferable object to transmit data.
The contents encapsulated in Transferable are stored in DataFlavors. You can access DataFlavors to obtain data.
Author: old wow's paw Attilax iron, EMAIL: 1466519819@qq.com
Reprinted please indicate Source: http://blog.csdn.net/attilax
2.. net blackhead dragging mechanism. A DragEnter event is required (write the DragDrop event separately without the drag function)
Idea: Use the DragEnter event to obtain the "information" of the dragged window (several files, some text, and so on ),
Parse "information" in the DragDrop event.
The AllowDrop attribute of the form must be set to true;
The DragEnter event must exist. (If you write a DragDrop event separately, the DragEnter event will not be dragged)
Private void Form1_DragEnter (object sender, DragEventArgs e)
{
If (e. Data. GetDataPresent (DataFormats. FileDrop ))
E. Effect = DragDropEffects. Link; // important code: Indicates data of the Link type, such as the file path.
Else e. Effect = DragDropEffects. None;
}
Private void Form1_DragDrop (object sender, DragEventArgs e)
{
String path = (System. Array) e. Data. GetData (DataFormats. FileDrop). GetValue (0). ToString ();
MessageBox. Show (path );
}
3. --- java blackhead dragging... the DragEnter event must exist (writing the Drop event alone will not have the drag function)
This feature AWT also provides Swing, but only utilizes it, because it has nothing to do with the interface.
Use the drag function in JAVA
Sun introduced some new methods in java2 to help implement the drag and drop function. These new classes are in the java. awt. dnd package.
JDK simplifies the drag-and-drop function of Swing. Different components provide drag-and-drop sources and targets. Here we will talk about the most used text fields, which can be dragged into the text, text can also be dragged out, which is very convenient.
However, this function is not enabled by default. You need to manually call the setDragEnabled method to make it take effect. Eg
4. Code
--- Form ini ()
Class SetImgSoftlinkGener extends JFrame implements Iskin, DropTargetListener
Frame. setDropTarget (new DropTarget (frame, DnDConstants. ACTION_COPY_OR_MOVE, this, true ));
@ Override
Public void dragEnter (DropTargetDragEvent dtde ){
System. out. println ("===== enter drag enter ");
// Get the type of object being transferred and determine
// Whether it is appropriate.
// CheckTransferType (dtde );
// Only accept a list of files
Boolean acceptableType = dtde
. IsDataFlavorSupported (DataFlavor. javaFileListFlavor );
// Accept or reject the drag.
// AcceptOrRejectDrag (dtde );
Int dropAction = dtde. getDropAction ();
Int sourceActions = dtde. getSourceActions ();
Boolean acceptedDrag = false;
// Reject if the object being transferred
// Or the operations available are not acceptable.
If (! AcceptableType
| (SourceActions & DnDConstants. ACTION_COPY_OR_MOVE) = 0 ){
// DnDUtils. debuuplintln ("Drop target rejecting drag ");
Dtde. rejectDrag ();
} Else if (dropAction & DnDConstants. ACTION_COPY_OR_MOVE) = 0 ){
// Not offering copy or move-suggest a copy
// DnDUtils. debuuplintln ("Drop target offering COPY ");
Dtde. acceptDrag (DnDConstants. ACTION_COPY );
AcceptedDrag = true;
} Else {
// Offering an acceptable operation: accept
// DnDUtils. debuuplintln ("Drop target accepting drag ");
Dtde. acceptDrag (dropAction );
AcceptedDrag = true;
}
// Return acceptedDrag;
}
@ Override
Public void drop (DropTargetDropEvent dtde ){
System. out. println ("===== enter drag drop ...");
// Check the drop action
If (dtde. getDropAction () & DnDConstants. ACTION_COPY_OR_MOVE )! = 0 ){
// Accept the drop and get the transfer data
Dtde. acceptDrop (dtde. getDropAction ());
Transferable transferable = dtde. getTransferable ();
System. out. println ("");
Try {
List <File> fileList = (List) transferable
. GetTransferData (DataFlavor. javaFileListFlavor );
// FileList. getClass (). toString ();
// String path = fileList [0]. getPath ();
String path = fileList. get (0). getPath ();
MsgBox. setTxt (path );
System. out. println (path );
} Catch (UnsupportedFlavorException | IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
// Dtde. dropComplete (result );
// Dtde. rejectDrop ();
}
}
5. Reference
Use the drag and drop feature in JAVA-gudong2945's column-blog channel-CSDN.NET.htm
(Impt) swing to achieve the drag effect (drag a local file to the text box of the program to display the content)-java learning exchange-blog channel-CSDN.NET.htm
Crazy JAVA handout --- chapter 12th: Swing programming (iii) drag-and-drop feature-TERRY's technical logs-blog channel-CSDN.NET.htm
(A bit useful) java Drag and drop( ) __javase__ .htm
(Simple) drag the Java Swing GUI file-it's not too late to start from the moment you understand it. -Blog channel-CSDN.NET.htm
C #. net drag to get the file path-attilax column-blog channel-CSDN.NET.htm
WinForm's mouse drag function (C #)
C # drag-and-drop operation-log of fumingkui @ 126-wangyi blog .htm
Winform(cform _ .nettutorial network-simple and professional .nettechnical website .htm