JavaFX中的DataFlavor无法正确识别

时间:2013-11-25 22:57:56

标签: drag-and-drop imageview javafx-2

当D& D从Swing到JavaFX的自定义对象时,我遇到了一个问题,我想知道我是做错了什么还是可能是Java FX错误。

我的可转让定义如下:

public class TransferableEmployee implements Transferable {

  public static final DataFlavor EMPLOYEE_FLAVOR = new DataFlavor(Employee[].class, "Employee");
  public static final DataFlavor DEFINITION_FLAVOR = new DataFlavor(PropertyDefinition[].class, "Definition");

  private static final DataFlavor FFLAVORS [] = {EMPLOYEE_FLAVOR, DEFINITION_FLAVOR};

  private Employee[] employees;
  private PropertyDefinition[] propertyDefinitions;

  public MintTransferableEmployee(Employee[] employees, PropertyDefinition[] propertyDefinitions) {
    this.employees = employees != null ? employees.clone() : null;
    this.propertyDefinitions = propertyDefinitions != null ? propertyDefinitions.clone() : null;
  }

  public DataFlavor[] getTransferDataFlavors() {
    return FFLAVORS.clone();
  }

  public Object getTransferData(DataFlavor aFlavor) throws UnsupportedFlavorException  {

    Object returnObject = null;
    if (aFlavor.equals(EMPLOYEE_FLAVOR)) {
      returnObject = employees;
    }
    else if(aFlavor.equals(DEFINITION_FLAVOR)){
      returnObject = propertyDefinitions;
    }
    else{
      throw new UnsupportedFlavorException(aFlavor);
    }
    return returnObject;
  }

  public boolean isDataFlavorSupported(DataFlavor aFlavor) {
    boolean lReturnValue = false;
    for (int i=0, n=FFLAVORS.length; i<n; i++) {
      if (aFlavor.equals(FFLAVORS[i])) {
        lReturnValue = true;
        break;
      }
    }
    return lReturnValue;
  }
}

我已经创建了一个imageView(FX组件),我在其中添加了setOnDragOver,如下所示:

employeePhotoImageView.setOnDragOver(new EventHandler<DragEvent>() {

      @Override
      public void handle(DragEvent event) {
        System.out.println("dragOver");
        event.getDragboard().getContentTypes();
        event.getDragboard().getContent(DataFormat.lookupMimeType("application/x-java-serialized-object"));

      }
    });

getContentTypes()返回带有[[application / x-java-serialized-object]]的Map,所以现在我尝试获取Content,这只返回PropertyDefinition列表,但根本不返回任何Employee(在这种情况,是我需要的。)

如果我删除了transferable中PropertyDefinition的数据,则会在getContent(DataFormat)方法中返回该雇员。

对我来说,这意味着JavaFX仅适用于1个DataFlavor,或者它只是返回Transferable中找到的最后一种风格。

有关此的任何线索吗?

先谢谢...

0 个答案:

没有答案