高分求教,关于Drag and drop

时间:2022-08-30 14:10:27
下面是我的一个测试程序关于Drag and drop的,在Jdk1.3下DropTargetListener的Drop方法始终都不被调用,但是在JDK1.4下却可以被正常调用,不知道为什么,开始的时候我用的是表格,我以为是表格的问题,但是后来我改成JTextField还是存在这个问题,请高手指教..........必重分相与.......还有请看http://expert.csdn.net/Expert/topic/1527/1527245.xml?temp=4.076785E-02分都给大家.回复者有分.

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.dnd.*;
import java.awt.datatransfer.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: </p>
 * @author unascribed
 * @version 1.0
 */

public class FrmTest1 extends JFrame {
    private JButton jButton1 = new JButton();
    String sTbl1 = "tbl1";
    String sTbl2 = "tbl2";
    String[] sCols1 = {"a1","b1","c1","d","e","f","g","h","i"};
    String[] sCols2 = {"a2","b2","c2"};
    private JTextField jTable1 = new JTextField(sTbl1);
    private JTextField jTable2 = new JTextField(sTbl2);
    public FrmTest1() {
        try {
            jbInit();
            //ウィンドウを*に配置
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            Dimension frameSize = this.getSize();
            if (frameSize.height > screenSize.height)
                frameSize.height = screenSize.height;
            if (frameSize.width > screenSize.width)
                frameSize.width = screenSize.width;
            this.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
            this.setVisible(true);
            new DropTarget(jTable2, DnDConstants.ACTION_LINK,new TblDropListenre());
            DragSource dragSource = DragSource.getDefaultDragSource();
            dragSource.createDefaultDragGestureRecognizer(
                    jTable1, // component where drag originates
                    DnDConstants.ACTION_LINK, // actions
                    new TblGesture());
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        FrmTest1 frmTest1 = new FrmTest1();
        frmTest1.show();
    }
    private void jbInit() throws Exception {
        this.setSize(400,300);
        jButton1.setBounds(new Rectangle(41, 29, 58, 16));
        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
                jButton1_actionPerformed(e);
            }
        });
        this.getContentPane().setLayout(null);
        jTable1.setBounds(new Rectangle(37, 89, 136, 98));
        jTable2.setBounds(new Rectangle(199, 87, 146, 102));
        this.getContentPane().add(jButton1, null);
        this.getContentPane().add(jTable1, null);
        this.getContentPane().add(jTable2, null);
    }

    void jButton1_actionPerformed(ActionEvent e) {

    }
    class TblDragListener implements DragSourceListener{
        public void dragEnter(DragSourceDragEvent dsde){
            System.out.println("dragEnter(DragSourceDragEvent dsde)");
        }
        public void dragOver(DragSourceDragEvent dsde){
            System.out.println("dragOver(DragSourceDragEvent dsde)");
        }
        public void dragDropEnd(DragSourceDropEvent dsde){
            System.out.println("dragDropEnd(DragSourceDropEvent dsde)");
        }
        public void dropActionChanged(DragSourceDragEvent dsde){
            System.out.println("dropActionChanged(DragSourceDragEvent dsde)");
        }
        public void dragExit(DragSourceEvent dse){
            System.out.println("dragExit(DragSourceEvent dse)");
        }
    }
    class TblDropListenre implements DropTargetListener{
        public void dragEnter(DropTargetDragEvent dtde){
            System.out.println("dragEnter(DropTargetDragEvent dtde)");
        }
        public void dragOver(DropTargetDragEvent dtde){
            System.out.println("dragOver(DropTargetDragEvent dtde)");
        }
        public void dropActionChanged(DropTargetDragEvent dtde){
            System.out.println("dropActionChanged(DropTargetDragEvent dtde)");
        }
        public void dragExit(DropTargetEvent dte){
            System.out.println("dragExit(DropTargetEvent dte)");
        }
        public void drop(DropTargetDropEvent e){
            System.out.println("drop(DropTargetDropEvent e)");
        }
    }
    class TblGesture implements DragGestureListener{
        public void dragGestureRecognized(DragGestureEvent dge){
            System.out.println("dragGestureRecognized(DragGestureEvent dge)");
            int i = 1;
            DragData dd = new DragData(null,i);
            StrDataTransfer dataTransfer =  new StrDataTransfer(dd);
            if(dataTransfer == null){
                return;
            }
            dge.startDrag(DragSource.DefaultLinkDrop,dataTransfer,new TblDragListener());
        }
    }
    class StrDataTransfer implements Transferable{
        DragData data;
        DataFlavor flavor;
        public StrDataTransfer(JPanel tbl,int col){
            this.data = new DragData(tbl,col);
            try{
                flavor = new DataFlavor(Class.forName("FrmTest1$DragData"),"DragData");
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        public StrDataTransfer(DragData df){
            this.data = df;
            try{
                flavor = new DataFlavor(Class.forName("FrmTest1$DragData"),"DragData");
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        public Object getTransferData(DataFlavor flavor) {
            if(isDataFlavorSupported(flavor))
                return data;
            return null;
        }
        public DataFlavor[] getTransferDataFlavors(){
            DataFlavor[] df = {flavor};
            return df;
        }
        public boolean isDataFlavorSupported(DataFlavor flavor1){
            if(flavor1.equals(flavor))
                return true;
            return false;
        }
    }
    class DragData{
        private JPanel table;
        private int iCol;
        public DragData(JPanel tbl,int iCol){
            this.table = tbl;
            this.iCol = iCol;
        }
        public JPanel getTable(){
            return table;
        }
        public int getCol(){
            return iCol;
        }
    }
}

13 个解决方案

#1


大家快来帮帮忙呀..........

#2


new DropTarget(jTable2, DnDConstants.ACTION_LINK,new TblDropListenre());
改为
new DropTarget(jTable2,new TblDropListenre());

#3


我试了,不行呀
还是没有调用Drop方法.

#4


我也试了呀,可以了
dragEnter(DropTargetDragEvent dtde)

dragOver(DropTargetDragEvent dtde)

dragOver(DropTargetDragEvent dtde)

dragOver(DropTargetDragEvent dtde)

dragOver(DropTargetDragEvent dtde)

dragOver(DropTargetDragEvent dtde)

dragOver(DropTargetDragEvent dtde)

dragOver(DropTargetDragEvent dtde)

dragOver(DropTargetDragEvent dtde)

dragOver(DropTargetDragEvent dtde)

dragOver(DropTargetDragEvent dtde)

dragOver(DropTargetDragEvent dtde)

dragOver(DropTargetDragEvent dtde)

dragOver(DropTargetDragEvent dtde)

dragOver(DropTargetDragEvent dtde)

dragOver(DropTargetDragEvent dtde)

dragOver(DropTargetDragEvent dtde)

dragOver(DropTargetDragEvent dtde)

dragOver(DropTargetDragEvent dtde)

dragOver(DropTargetDragEvent dtde)

dragOver(DropTargetDragEvent dtde)

dragExit(DropTargetEvent dte)

drop(DropTargetDropEvent e)

#5


改过的文件还给你,从IE选段文字拖到右边那个table里是上面的结果,鼠标指针是带加号的。jdk1.3.1

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.dnd.*;
import java.awt.datatransfer.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: </p>
 * @author unascribed
 * @version 1.0
 */

public class FrmTest1 extends JFrame {
    private JButton jButton1 = new JButton();
    String sTbl1 = "tbl1";
    String sTbl2 = "tbl2";
    String[] sCols1 = {"a1","b1","c1","d","e","f","g","h","i"};
    String[] sCols2 = {"a2","b2","c2"};
    private JTextField jTable1 = new JTextField(sTbl1);
    private JTextField jTable2 = new JTextField(sTbl2);
    public FrmTest1() {
        try {
            jbInit();
            //ウィンドウを*に配置
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            Dimension frameSize = this.getSize();
            if (frameSize.height > screenSize.height)
                frameSize.height = screenSize.height;
            if (frameSize.width > screenSize.width)
                frameSize.width = screenSize.width;
            this.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
            this.setVisible(true);
            new DropTarget(jTable2,new TblDropListenre());
            DragSource dragSource = DragSource.getDefaultDragSource();
            dragSource.createDefaultDragGestureRecognizer(
                    jTable1, // component where drag originates
                    DnDConstants.ACTION_LINK, // actions
                    new TblGesture());
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        FrmTest1 frmTest1 = new FrmTest1();
        frmTest1.show();
    }
    private void jbInit() throws Exception {
        this.setSize(400,300);
        jButton1.setBounds(new Rectangle(41, 29, 58, 16));
        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
                jButton1_actionPerformed(e);
            }
        });
        this.getContentPane().setLayout(null);
        jTable1.setBounds(new Rectangle(37, 89, 136, 98));
        jTable2.setBounds(new Rectangle(199, 87, 146, 102));
        this.getContentPane().add(jButton1, null);
        this.getContentPane().add(jTable1, null);
        this.getContentPane().add(jTable2, null);
    }

    void jButton1_actionPerformed(ActionEvent e) {

    }
    class TblDragListener implements DragSourceListener{
        public void dragEnter(DragSourceDragEvent dsde){
            System.out.println("dragEnter(DragSourceDragEvent dsde)");
        }
        public void dragOver(DragSourceDragEvent dsde){
            System.out.println("dragOver(DragSourceDragEvent dsde)");
        }
        public void dragDropEnd(DragSourceDropEvent dsde){
            System.out.println("dragDropEnd(DragSourceDropEvent dsde)");
        }
        public void dropActionChanged(DragSourceDragEvent dsde){
            System.out.println("dropActionChanged(DragSourceDragEvent dsde)");
        }
        public void dragExit(DragSourceEvent dse){
            System.out.println("dragExit(DragSourceEvent dse)");
        }
    }
    class TblDropListenre implements DropTargetListener{
        public void dragEnter(DropTargetDragEvent dtde){
            System.out.println("dragEnter(DropTargetDragEvent dtde)");
        }
        public void dragOver(DropTargetDragEvent dtde){
            System.out.println("dragOver(DropTargetDragEvent dtde)");
        }
        public void dropActionChanged(DropTargetDragEvent dtde){
            System.out.println("dropActionChanged(DropTargetDragEvent dtde)");
        }
        public void dragExit(DropTargetEvent dte){
            System.out.println("dragExit(DropTargetEvent dte)");
        }
        public void drop(DropTargetDropEvent e){
            System.out.println("drop(DropTargetDropEvent e)");
        }
    }
    class TblGesture implements DragGestureListener{
        public void dragGestureRecognized(DragGestureEvent dge){
            System.out.println("dragGestureRecognized(DragGestureEvent dge)");
            int i = 1;
            DragData dd = new DragData(null,i);
            StrDataTransfer dataTransfer =  new StrDataTransfer(dd);
            if(dataTransfer == null){
                return;
            }
            dge.startDrag(DragSource.DefaultLinkDrop,dataTransfer,new TblDragListener());
        }
    }
    class StrDataTransfer implements Transferable{
        DragData data;
        DataFlavor flavor;
        public StrDataTransfer(JPanel tbl,int col){
            this.data = new DragData(tbl,col);
            try{
                flavor = new DataFlavor(Class.forName("FrmTest1$DragData"),"DragData");
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        public StrDataTransfer(DragData df){
            this.data = df;
            try{
                flavor = new DataFlavor(Class.forName("FrmTest1$DragData"),"DragData");
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        public Object getTransferData(DataFlavor flavor) {
            if(isDataFlavorSupported(flavor))
                return data;
            return null;
        }
        public DataFlavor[] getTransferDataFlavors(){
            DataFlavor[] df = {flavor};
            return df;
        }
        public boolean isDataFlavorSupported(DataFlavor flavor1){
            if(flavor1.equals(flavor))
                return true;
            return false;
        }
    }
    class DragData{
        private JPanel table;
        private int iCol;
        public DragData(JPanel tbl,int iCol){
            this.table = tbl;
            this.iCol = iCol;
        }
        public JPanel getTable(){
            return table;
        }
        public int getCol(){
            return iCol;
        }
    }
}

#6


如果是从IE选段文字拖到右边那个table里,这不改就是这个结果的
你试过从左边拖到右边吗?
还是不行的....,Drop还是出不来的

#7


不对啊,没改以前从ie里拖也不触发drop,而且拖动的时候鼠标是禁止放下的图标。
如果从左边拖不到右边,那么是拖动源的问题。

#8


拖动源好象没什么问题的,因为他的方法都正确的调用了
是的,不好意思,没改以前从ie里拖确实不触发drop,因为我只接受Link操作,毛病到底出在哪呢?哎~~~~~~~~~~~~~~~~~~~~

#9


我觉得就是拖动源的问题,你试没试过把拖动源里的东西拖到word?
左边的框一动鼠标就异常啊,好象是数据类型不对。

#10


终于搞定了,呵呵
原来是传递的数据类型必须实现序列化接口
让DragData类implements Serializable就可以了
Faint............Java真是变态.....
好在JDK1.4中已经正常了.
谢谢bjzhanghao(八进制)老兄鼎立相帮助............
结贴.

#11


:)

#12


:)
0,90,10,0,100,0,0,0,0,0
贴子回复次数大于跟给分次数 
这是什么意思呀?
怎么不让我结帖呀.....

#13


不会吧……555哭死了

#1


大家快来帮帮忙呀..........

#2


new DropTarget(jTable2, DnDConstants.ACTION_LINK,new TblDropListenre());
改为
new DropTarget(jTable2,new TblDropListenre());

#3


我试了,不行呀
还是没有调用Drop方法.

#4


我也试了呀,可以了
dragEnter(DropTargetDragEvent dtde)

dragOver(DropTargetDragEvent dtde)

dragOver(DropTargetDragEvent dtde)

dragOver(DropTargetDragEvent dtde)

dragOver(DropTargetDragEvent dtde)

dragOver(DropTargetDragEvent dtde)

dragOver(DropTargetDragEvent dtde)

dragOver(DropTargetDragEvent dtde)

dragOver(DropTargetDragEvent dtde)

dragOver(DropTargetDragEvent dtde)

dragOver(DropTargetDragEvent dtde)

dragOver(DropTargetDragEvent dtde)

dragOver(DropTargetDragEvent dtde)

dragOver(DropTargetDragEvent dtde)

dragOver(DropTargetDragEvent dtde)

dragOver(DropTargetDragEvent dtde)

dragOver(DropTargetDragEvent dtde)

dragOver(DropTargetDragEvent dtde)

dragOver(DropTargetDragEvent dtde)

dragOver(DropTargetDragEvent dtde)

dragOver(DropTargetDragEvent dtde)

dragExit(DropTargetEvent dte)

drop(DropTargetDropEvent e)

#5


改过的文件还给你,从IE选段文字拖到右边那个table里是上面的结果,鼠标指针是带加号的。jdk1.3.1

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.dnd.*;
import java.awt.datatransfer.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: </p>
 * @author unascribed
 * @version 1.0
 */

public class FrmTest1 extends JFrame {
    private JButton jButton1 = new JButton();
    String sTbl1 = "tbl1";
    String sTbl2 = "tbl2";
    String[] sCols1 = {"a1","b1","c1","d","e","f","g","h","i"};
    String[] sCols2 = {"a2","b2","c2"};
    private JTextField jTable1 = new JTextField(sTbl1);
    private JTextField jTable2 = new JTextField(sTbl2);
    public FrmTest1() {
        try {
            jbInit();
            //ウィンドウを*に配置
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            Dimension frameSize = this.getSize();
            if (frameSize.height > screenSize.height)
                frameSize.height = screenSize.height;
            if (frameSize.width > screenSize.width)
                frameSize.width = screenSize.width;
            this.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
            this.setVisible(true);
            new DropTarget(jTable2,new TblDropListenre());
            DragSource dragSource = DragSource.getDefaultDragSource();
            dragSource.createDefaultDragGestureRecognizer(
                    jTable1, // component where drag originates
                    DnDConstants.ACTION_LINK, // actions
                    new TblGesture());
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        FrmTest1 frmTest1 = new FrmTest1();
        frmTest1.show();
    }
    private void jbInit() throws Exception {
        this.setSize(400,300);
        jButton1.setBounds(new Rectangle(41, 29, 58, 16));
        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
                jButton1_actionPerformed(e);
            }
        });
        this.getContentPane().setLayout(null);
        jTable1.setBounds(new Rectangle(37, 89, 136, 98));
        jTable2.setBounds(new Rectangle(199, 87, 146, 102));
        this.getContentPane().add(jButton1, null);
        this.getContentPane().add(jTable1, null);
        this.getContentPane().add(jTable2, null);
    }

    void jButton1_actionPerformed(ActionEvent e) {

    }
    class TblDragListener implements DragSourceListener{
        public void dragEnter(DragSourceDragEvent dsde){
            System.out.println("dragEnter(DragSourceDragEvent dsde)");
        }
        public void dragOver(DragSourceDragEvent dsde){
            System.out.println("dragOver(DragSourceDragEvent dsde)");
        }
        public void dragDropEnd(DragSourceDropEvent dsde){
            System.out.println("dragDropEnd(DragSourceDropEvent dsde)");
        }
        public void dropActionChanged(DragSourceDragEvent dsde){
            System.out.println("dropActionChanged(DragSourceDragEvent dsde)");
        }
        public void dragExit(DragSourceEvent dse){
            System.out.println("dragExit(DragSourceEvent dse)");
        }
    }
    class TblDropListenre implements DropTargetListener{
        public void dragEnter(DropTargetDragEvent dtde){
            System.out.println("dragEnter(DropTargetDragEvent dtde)");
        }
        public void dragOver(DropTargetDragEvent dtde){
            System.out.println("dragOver(DropTargetDragEvent dtde)");
        }
        public void dropActionChanged(DropTargetDragEvent dtde){
            System.out.println("dropActionChanged(DropTargetDragEvent dtde)");
        }
        public void dragExit(DropTargetEvent dte){
            System.out.println("dragExit(DropTargetEvent dte)");
        }
        public void drop(DropTargetDropEvent e){
            System.out.println("drop(DropTargetDropEvent e)");
        }
    }
    class TblGesture implements DragGestureListener{
        public void dragGestureRecognized(DragGestureEvent dge){
            System.out.println("dragGestureRecognized(DragGestureEvent dge)");
            int i = 1;
            DragData dd = new DragData(null,i);
            StrDataTransfer dataTransfer =  new StrDataTransfer(dd);
            if(dataTransfer == null){
                return;
            }
            dge.startDrag(DragSource.DefaultLinkDrop,dataTransfer,new TblDragListener());
        }
    }
    class StrDataTransfer implements Transferable{
        DragData data;
        DataFlavor flavor;
        public StrDataTransfer(JPanel tbl,int col){
            this.data = new DragData(tbl,col);
            try{
                flavor = new DataFlavor(Class.forName("FrmTest1$DragData"),"DragData");
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        public StrDataTransfer(DragData df){
            this.data = df;
            try{
                flavor = new DataFlavor(Class.forName("FrmTest1$DragData"),"DragData");
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        public Object getTransferData(DataFlavor flavor) {
            if(isDataFlavorSupported(flavor))
                return data;
            return null;
        }
        public DataFlavor[] getTransferDataFlavors(){
            DataFlavor[] df = {flavor};
            return df;
        }
        public boolean isDataFlavorSupported(DataFlavor flavor1){
            if(flavor1.equals(flavor))
                return true;
            return false;
        }
    }
    class DragData{
        private JPanel table;
        private int iCol;
        public DragData(JPanel tbl,int iCol){
            this.table = tbl;
            this.iCol = iCol;
        }
        public JPanel getTable(){
            return table;
        }
        public int getCol(){
            return iCol;
        }
    }
}

#6


如果是从IE选段文字拖到右边那个table里,这不改就是这个结果的
你试过从左边拖到右边吗?
还是不行的....,Drop还是出不来的

#7


不对啊,没改以前从ie里拖也不触发drop,而且拖动的时候鼠标是禁止放下的图标。
如果从左边拖不到右边,那么是拖动源的问题。

#8


拖动源好象没什么问题的,因为他的方法都正确的调用了
是的,不好意思,没改以前从ie里拖确实不触发drop,因为我只接受Link操作,毛病到底出在哪呢?哎~~~~~~~~~~~~~~~~~~~~

#9


我觉得就是拖动源的问题,你试没试过把拖动源里的东西拖到word?
左边的框一动鼠标就异常啊,好象是数据类型不对。

#10


终于搞定了,呵呵
原来是传递的数据类型必须实现序列化接口
让DragData类implements Serializable就可以了
Faint............Java真是变态.....
好在JDK1.4中已经正常了.
谢谢bjzhanghao(八进制)老兄鼎立相帮助............
结贴.

#11


:)

#12


:)
0,90,10,0,100,0,0,0,0,0
贴子回复次数大于跟给分次数 
这是什么意思呀?
怎么不让我结帖呀.....

#13


不会吧……555哭死了