zoukankan      html  css  js  c++  java
  • SWT DragSource 和 DropTarget 托拉拽

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

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

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

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

    下面来看一下例子

    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添加一个监听,否则会报一下的错

    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)
    

      

    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
    				
    			}
    		});
    

      

  • 相关阅读:
    一个完整的SQL SERVER数据库全文索引的示例
    sqlhelper for access
    解决打开网站弹出下载文件
    18句话入门SQLServer XML
    c# winform 自动关闭messagebox 模拟回车
    jquery json
    用超级巡警批量清除被挂马的网页
    sqlhelper for sql
    SQL XML DML 数据修改
    post方法易错地方
  • 原文地址:https://www.cnblogs.com/oscar1987121/p/5216498.html
Copyright © 2011-2022 走看看