public class DropTargetTest
{ public static void main(String[] args)
{ JFrame frame = new DropTargetFrame();
frame.show();
}
}
class DropTargetFrame extends JFrame
{ public DropTargetFrame()
{ setTitle("DropTarget");
setSize(300, 300);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
} );
Container contentPane = getContentPane();
JTextArea textArea
= new JTextArea("Drop items into this text area.\n");
new DropTarget(textArea,
new TextDropTargetListener(textArea));
contentPane.add(new JScrollPane(textArea), "Center");
}
}
class TextDropTargetListener implements DropTargetListener
{ public TextDropTargetListener(JTextArea ta)
{ textArea = ta;
}
public void dragEnter(DropTargetDragEvent event)
{ int a = event.getDropAction();
if ((a & DnDConstants.ACTION_COPY) != 0)
textArea.append("ACTION_COPY\n");
if ((a & DnDConstants.ACTION_MOVE) != 0)
textArea.append("ACTION_MOVE\n");
if ((a & DnDConstants.ACTION_LINK) != 0)
textArea.append("ACTION_LINK\n");
if (!isDragAcceptable(event))
{ event.rejectDrag();
return;
}
}
public void dragExit(DropTargetEvent event)
{
}
public void dragOver(DropTargetDragEvent event)
{ // you can provide visual feedback here
}
public void dropActionChanged(DropTargetDragEvent event)
{ if (!isDragAcceptable(event))
{ event.rejectDrag();
return;
}
}
public void drop(DropTargetDropEvent event)
{ if (!isDropAcceptable(event))
{ event.rejectDrop();
return;
}
public boolean isDragAcceptable(DropTargetDragEvent event)
{ // usually, you check the available data flavors here
// in this program, we accept all flavors
return (event.getDropAction()
& DnDConstants.ACTION_COPY_OR_MOVE) != 0;
}
public boolean isDropAcceptable(DropTargetDropEvent event)
{ // usually, you check the available data flavors here
// in this program, we accept all flavors
return (event.getDropAction()
& DnDConstants.ACTION_COPY_OR_MOVE) != 0;
}