Example #1
0
 @Override
 public boolean canImport(TransferHandler.TransferSupport info) {
   boolean b =
       (info.getComponent() == table)
           && info.isDrop()
           && info.isDataFlavorSupported(localObjectFlavor);
   table.setCursor(b ? DragSource.DefaultMoveDrop : DragSource.DefaultMoveNoDrop);
   return b;
 }
  /**
   * Imports data if the specific flavor is supported
   *
   * @return Boolean
   */
  public boolean importData(TransferHandler.TransferSupport info) {
    if (!info.isDrop()) {
      return false;
    }

    // Check for String flavor
    if (!info.isDataFlavorSupported(DataFlavor.stringFlavor)) {
      System.out.println("Data flavor not supported.");
      return false;
    }

    JList.DropLocation dl = (JList.DropLocation) info.getDropLocation();
    DefaultListModel listModel = (DefaultListModel) list.getModel();
    int index = dl.getIndex();
    boolean insert = dl.isInsert();
    // Get the current string under the drop.
    // String value = (String)listModel.getElementAt(index);

    // Get the string that is being dropped.
    Transferable t = info.getTransferable();
    String data;
    try {
      data = (String) t.getTransferData(DataFlavor.stringFlavor);
    } catch (Exception e) {
      return false;
    }

    /**
     * This is commented out for the basicdemo.html tutorial page. * If you add this code snippet
     * back and delete the * "return false;" line, the list will accept drops * of type string.
     */
    // Perform the actual import.
    if (insert) {
      listModel.add(index, data);
    } else {
      listModel.set(index, data);
    }
    return true;
  }