Drag processing of Java text

Source: Internet
Author: User

Import javax.swing.*;
Import java.awt.dnd.*;
Import java.awt.datatransfer.*;

public class Draglabel extends JLabel implements droptargetlistener{

Public Draglabel (String str) {
Super (STR); Calling the parent class constructor
}

public void DragEnter (Droptargetdragevent evt) {
}
public void DragOver (Droptargetdragevent evt) {
}
public void dropactionchanged (Droptargetdragevent evt) {
}
public void Dragexit (Droptargetevent evt) {
}
public void Drop (droptargetdropevent evt) {//Drag action Processing
try{
Transferable trans = evt.gettransferable (); Be able to transferable objects
if (evt.isdataflavorsupported (Dataflavor.stringflavor)) {//whether to support dragging
Evt.acceptdrop (Evt.getdropaction ()); Accept Drag
string s = (string) trans.gettransferdata (Dataflavor.stringflavor); Get Drag data
SetText (s); Set the text of a label
Evt.dropcomplete (TRUE); End drag
}else{
Evt.rejectdrop (); Refuse to consignment

}
}catch (Exception err) {
Err.printstacktrace (); Output error message
}
}

}

Import java.awt.dnd.*;
Import java.awt.datatransfer.*;
Import javax.swing.*;

public class Draglist extends JList implements Draggesturelistener, dragsourcelistener{

DragSource Dragsource=dragsource.getdefaultdragsource (); Drag source
Public draglist (object[] data) {//constructor
Super (data);
int action = Dndconstants.action_copy_or_move; Drag type
Dragsource.createdefaultdraggesturerecognizer (This,action,this); Create drag recognition
}

public void draggesturerecognized (Draggestureevent dge) {
try{
Transferable trans = new StringSelection (This.getselectedvalue (). toString ()); Get the dragged transferable object
Dge.startdrag (Dragsource.defaultcopynodrop,trans,this); Start the drag operation
}catch (Exception err) {
Err.printstacktrace (); Output error message
}
}

public void DragEnter (Dragsourcedragevent dragsourcede) {//Drag entry processing
DragSourceContext DragSourceContext = Dragsourcede.getdragsourcecontext (); Get Drag Context Object
int action = Dragsourcede.getdropaction (); Get drag command
if ((action&dndconstants.action_copy)!=0)//judgment command type
Dragsourcecontext.setcursor (Dragsource.defaultcopydrop); Set Cursor type
Else
Dragsourcecontext.setcursor (Dragsource.defaultcopynodrop);
}
public void DragOver (Dragsourcedragevent dragsourcede) {
}
public void dropactionchanged (Dragsourcedragevent dragsourcede) {
}
public void Dragexit (Dragsourceevent dragsourcee) {
}
public void Dragdropend (Dragsourcedropevent dragsourcede) {
}
}
Instance:

Import java.awt.*;
Import javax.swing.*;
Import Javax.swing.JSplitPane;
Import java.awt.dnd.*;

Drag handling of text

public class Dragtextdemo extends jframe{

Public Dragtextdemo () {
Super ("Drag Processing of text"); Calling the parent class constructor

string[] data = {"One", "two", "three", "four"}; An array of characters used to construct a list box
Draglist list=new draglist (data); List Box instance
JTextArea jta=new JTextArea (8,20); Text Box instance
Draglabel label=new Draglabel ("drag target"); Label instance

Jta.setlinewrap (TRUE); Set up Wrap Line
Jta.setdragenabled (TRUE); text box to drag
New DropTarget (Label,dndconstants.action_copy,label); Instantiate drag target as label

Container Container=getcontentpane (); Get the container
JSplitPane split=new JSplitPane (jsplitpane.horizontal_split); Instantiating the separator panel
Split.setdividerlocation (140); Set the separator position
Split.add (list); Add Component to Separator panel
Split.add (JTA);
Container.add (Split,borderlayout.center); Adding components to the container
Container.add (Label,borderlayout.south);

SetSize (300,150); Set Window size
SetVisible (TRUE); Set window to Visual
Setdefaultcloseoperation (Jframe.exit_on_close); Exit Program when window closes
}

public static void Main (string[] args) {
New Dragtextdemo ();
}
}
Effect Chart:

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.