文档

Java™ 教程-Java Tutorials 中文版
TransferSupport 类
Trail: Creating a GUI With JFC/Swing
Lesson: Drag and Drop and Data Transfer

TransferSupport 类

The TransferSupport class serves two functions. As the name suggests, its first function is to support the transfer process and for that purpose it provides several utility methods used to access the details of the data transfer. The following list shows the methods that can be used to obtain information from the TransferHandler. Several of these methods are related to drop actions, which will be discussed in Setting the Drop Mode.

Sample Import Methods

Now that you are familiar with the TransferSupport utility methods, let us look at sample canImport and importData methods:

public boolean canImport(TransferSupport supp) {
    // Check for String flavor
    if (!supp.isDataFlavorSupported(stringFlavor)) {
        return false;
    }

    // Fetch the drop location
    DropLocation loc = supp.getDropLocation();

    // Return whether we accept the location
    return shouldAcceptDropLocation(loc);
}

public boolean importData(TransferSupport supp) {
    if (!canImport(sup)) {
        return false;
    }

    // Fetch the Transferable and its data
    Transferable t = supp.getTransferable();
    String data = t.getTransferData(stringFlavor);

    // Fetch the drop location
    DropLocation loc = supp.getDropLocation();

    // Insert the data at this location
    insertAt(loc, data);

    return true;
}

Next we look at how you can set the drop mode for selected components.


Previous page: Import Methods
Next page: Setting the Drop Mode