DataFlavor自定义类用法

时间:2017-02-04 07:28:31

标签: java swing drag-and-drop

我想用我的类(Item.class)

创建一个自定义dataFlavor
@Override
public synchronized void drop(DropTargetDropEvent dtde) {                
    DataFlavor itemFlavor = new DataFlavor(Item.class, Item.class.getSimpleName());               
    try{
        System.out.println(dtde.getTransferable().getTransferData(itemFlavor));
    }catch(UnsupportedFlavorException | IOException e){
        e.printStackTrace();
    }

}

Item.class

public class Item {
    private String classFile;
    private String imgFile;
    private String imgPath;    

    public Item(String classFile, String imgFile, String imgPath){
        this.classFile = classFile;
        this.imgFile = imgFile;
        this.imgPath = imgPath;
    }

    public String getImgFile() {
        return imgFile;
    }

    public void setImgFile(String imgFile) {
        this.imgFile = imgFile;
    }   

    public String getClassFile() {
        return classFile;
    }

    public void setClassFile(String classFile) {
        this.classFile = classFile;
    }

    public String getImgPath() {
        return imgPath;
    }

    public void setImgPath(String imgPath) {
        this.imgPath = imgPath;
    }
}

但是我收到了一个错误:java.awt.datatransfer.UnsupportedFlavorException: Item

你能说出错了吗?

我正在使用此文档https://docs.oracle.com/javase/tutorial/uiswing/dnd/dataflavor.html

示例,演示问题DND Test Project

要获得错误,请尝试从JTable拖动到JLayeredPane

0 个答案:

没有答案