Atitit. d&d drag&drop drag function c#.net java Swing comparison and implementation summary
1. Implementing a d&d operation typically consists of three steps :1
2. NET blackheads drag mechanism . Must have DragEnter event (individual write DragDrop event is not drag-and-drop function) 2
3. Drag the ---java blackheads. There must be a DragEnter event (a separate write drop event will not have a drag function) 2
4. Code 3
5. Reference 5
1. Implement ad&doperation typically consists of three steps:
First, a drag source is implemented, and the drag source is associated with the corresponding component.
The second step is to achieve a drag target, which is used to achieve the acceptance of the Drag object.
The third step is to implement a data transfer object that encapsulates the dragged data
_____________________ _____________________
| | | |
| DragSource component| | DropTarget component|
|_____________________| |____________________|
| |
|____________transferable data_________________|
The transferable interface implements an object that guarantees DropTarget component to read the information contained in the dragged object.
If you are implementing a drag-and-drop in the same virtual machine, DragSource Component will pass a reference to DropTarget Component
But if data is passed between different JVMs or between the JVM and the local system, we must implement a transferable object to pass the data
The contents of transferable are stored in the dataflavors, and the user can access the dataflavors to obtain the data.
Author :: Old Wow's paw attilax ayron, email:[email protected]
Reprint please indicate source: Http://blog.csdn.net/attilax
2.. netthe drag mechanism of blackheads.Must have DragEnter event (individual write DragDrop event is not drag-and-drop function)
Idea: Get the "info" dragged into the window via the DragEnter event (which can be several files, some text, etc.),
The "information" is parsed in the DragDrop event.
The AllowDrop property of the form must be set to true;
and must have a DragEnter event (separate write DragDrop event is not drag-and-drop function)
private void Form1_dragenter (object sender, DragEventArgs e)
{
if (E.data.getdatapresent (DataFormats.FileDrop))
E.effect = Dragdropeffects.link; Important code: Indicates a link type of data, such as a 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.---javathe drag of the blackheads..There must be a DragEnter event (a separate write drop event will not have a drag function)
This feature AWT also offers swing words that just take advantage of this because it doesn't have anything to do with the interface.
Using drag-and-drop functionality in Java
Sun has introduced new methods in JAVA2 to help with drag-and-drop functionality, and these new classes are in the JAVA.AWT.DND package
JDK1.4 began to simplify Swing 's Drag-and-drop functionality, with different components providing drag-and-drop sources and drag-and-drop targets, with the largest number of text fields available, with the ability to drag text in and out of the text.
However, this feature is not enabled by default and you should call the setdragenabled method manually to make it effective. 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 is not acceptable.
if (! Acceptabletype
|| (sourceactions & dndconstants. Action_copy_or_move ) = = 0) {
DNDUTILS.DEBUGPRINTLN ("Drop Target rejecting drag");
dtde . Rejectdrag ();
} else if ((dropaction & dndconstants. Action_copy_or_move ) = = 0) {
Not offering copy or move-suggest a copy
DNDUTILS.DEBUGPRINTLN ("Drop target offering COPY");
dtde . Acceptdrag (dndconstants. action_copy );
Accepteddrag = true ;
} else {
Offering an acceptable operation:accept
DNDUTILS.DEBUGPRINTLN ("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 (dtdegetdropaction () & 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
Using drag-and-drop functionality in Java-gudong2945 's column-Blog channel-CSDN.NET.htm
(impt) Swing for drag-and-drop effects (drag local files to display content in the program's text box)-Java Learning Communication-Blog channel-CSDN.NET.htm
Crazy Java Handout---12th: Swing Programming (iii) Drag-and-drop functionality-Terry's technical Log-blog channel-CSDN.NET.htm
Java Drag and drop (drag) a simple example _javase bar _ Baidu paste. htm
(simple) Java Swing GUI file drag-and-drop-from the moment you understand it, it's not too late to sail. -Blog Channel-CSDN.NET.htm
C#.net drag-and-drop implementation get file path-attilax column-Blog channel-CSDN.NET.htm
WinForm Mouse drag and drop function (C #)
Drag-and-drop operations in C #-[email protected]-blog-netease blogs. htm
WinForm (C #) Drag and drop implementation to get the file path _. NET Tutorial Network--Simple and professional. NET technology Web site. htm
Atitit. D&d Drag&drop Drag function c#.net Java Swing comparison and implementation summary