SWT DragSource 和 DropTarget 托拉拽

SWT 托拉拽主要由3个部分组成

DragSource - 拖拉的来源封装在这个类当中

DropTarget - 粘贴的对象封装在这个类当中

Transfer - 他们之间传输的中介就靠这个

下面来看一下例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.StatusLineManager;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DragSource;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.DragSourceListener;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.DropTargetListener;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
 
public class DragAndDrog extends ApplicationWindow {
    private Text dropText;
    private Text dragText;
 
    /**
     * Create the application window.
     */
    public DragAndDrog() {
        super(null);
        createActions();
        addToolBar(SWT.FLAT | SWT.WRAP);
        addMenuBar();
        addStatusLine();
    }
 
    /**
     * Create contents of the application window.
     * @param parent
     */
    @Override
    protected Control createContents(Composite parent) {
        Composite container = new Composite(parent, SWT.NONE);
         
        dropText = new Text(container, SWT.BORDER);
        dropText.setBounds(46, 48, 73, 21);
         
        DropTarget target = new DropTarget(dropText,DND.DROP_MOVE);
        target.setTransfer(new Transfer[]{TextTransfer.getInstance()});
        target.addDropListener(new DropTargetListener() {
             
            @Override
            public void dropAccept(DropTargetEvent event) {
                // TODO Auto-generated method stub
                 
            }
             
            @Override
            public void drop(DropTargetEvent event) {
                // TODO Auto-generated method stub
                dropText.setText(String.valueOf(event.data));
            }
             
            @Override
            public void dragOver(DropTargetEvent event) {
                // TODO Auto-generated method stub
                 
            }
             
            @Override
            public void dragOperationChanged(DropTargetEvent event) {
                // TODO Auto-generated method stub
                 
            }
             
            @Override
            public void dragLeave(DropTargetEvent event) {
                // TODO Auto-generated method stub
                 
            }
             
            @Override
            public void dragEnter(DropTargetEvent event) {
                // TODO Auto-generated method stub
                 
            }
        });
         
        dragText = new Text(container, SWT.BORDER);
        dragText.setBounds(198, 48, 73, 21);
         
        DragSource dragSource = new DragSource(dragText, DND.DROP_MOVE);
        dragSource.setTransfer(new Transfer[]{TextTransfer.getInstance()});
        dragSource.addDragListener(new DragSourceListener() {
             
            @Override
            public void dragStart(DragSourceEvent event) {
                // TODO Auto-generated method stub
                 
            }
             
            @Override
            public void dragSetData(DragSourceEvent event) {
                event.data = dragText.getText();
                 
            }
             
            @Override
            public void dragFinished(DragSourceEvent event) {
                // TODO Auto-generated method stub
                 
            }
        });
 
        return container;
    }
 
    /**
     * Create the actions.
     */
    private void createActions() {
        // Create the actions
    }
 
    /**
     * Create the menu manager.
     * @return the menu manager
     */
    @Override
    protected MenuManager createMenuManager() {
        MenuManager menuManager = new MenuManager("menu");
        return menuManager;
    }
 
    /**
     * Create the toolbar manager.
     * @return the toolbar manager
     */
    @Override
    protected ToolBarManager createToolBarManager(int style) {
        ToolBarManager toolBarManager = new ToolBarManager(style);
        return toolBarManager;
    }
 
    /**
     * Create the status line manager.
     * @return the status line manager
     */
    @Override
    protected StatusLineManager createStatusLineManager() {
        StatusLineManager statusLineManager = new StatusLineManager();
        return statusLineManager;
    }
 
    /**
     * Launch the application.
     * @param args
     */
    public static void main(String args[]) {
        try {
            DragAndDrog window = new DragAndDrog();
            window.setBlockOnOpen(true);
            window.open();
            Display.getCurrent().dispose();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * Configure the shell.
     * @param newShell
     */
    @Override
    protected void configureShell(Shell newShell) {
        super.configureShell(newShell);
        newShell.setText("New Application");
    }
 
    /**
     * Return the initial size of the window.
     */
    @Override
    protected Point getInitialSize() {
        return new Point(450, 300);
    }
}

  

在上面的代码中,主要的是,必须为DragSource添加一个监听,否则会报一下的错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
org.eclipse.swt.SWTException: Data does not have correct format for type
    at org.eclipse.swt.dnd.DND.error(DND.java:276)
    at org.eclipse.swt.dnd.DND.error(DND.java:227)
    at org.eclipse.swt.dnd.TextTransfer.javaToNative(TextTransfer.java:67)
    at org.eclipse.swt.dnd.DragSource.GetData(DragSource.java:472)
    at org.eclipse.swt.dnd.DragSource.access$7(DragSource.java:436)
    at org.eclipse.swt.dnd.DragSource$4.method3(DragSource.java:259)
    at org.eclipse.swt.internal.ole.win32.COMObject.callback3(COMObject.java:92)
    at org.eclipse.swt.internal.ole.win32.COM.VtblCall(Native Method)
    at org.eclipse.swt.internal.ole.win32.IDataObject.GetData(IDataObject.java:25)
    at org.eclipse.swt.dnd.Transfer.getData(Transfer.java:44)
    at org.eclipse.swt.dnd.TextTransfer.nativeToJava(TextTransfer.java:127)
    at org.eclipse.swt.dnd.DropTarget.Drop(DropTarget.java:443)
    at org.eclipse.swt.dnd.DropTarget$3.method6(DropTarget.java:258)
    at org.eclipse.swt.internal.ole.win32.COMObject.callback6(COMObject.java:119)
    at org.eclipse.swt.internal.ole.win32.COM.DoDragDrop(Native Method)
    at org.eclipse.swt.dnd.DragSource.drag(DragSource.java:363)
    at org.eclipse.swt.dnd.DragSource.access$0(DragSource.java:289)
    at org.eclipse.swt.dnd.DragSource$1.handleEvent(DragSource.java:172)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
    at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4169)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3758)
    at org.eclipse.jface.window.Window.runEventLoop(Window.java:825)
    at org.eclipse.jface.window.Window.open(Window.java:801)
    at cccccc.DragAndDrog.main(DragAndDrog.java:165)

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
dragSource.addDragListener(new DragSourceListener() {
             
            @Override
            public void dragStart(DragSourceEvent event) {
                // TODO Auto-generated method stub
                 
            }
             
            @Override
            public void dragSetData(DragSourceEvent event) {
                event.data = dragText.getText();
                 
            }
             
            @Override
            public void dragFinished(DragSourceEvent event) {
                // TODO Auto-generated method stub
                 
            }
        });

  

posted @   长命百岁  阅读(523)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示