下面列出了怎么用java.awt.dnd.DropTarget的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void addDropTarget(DropTarget dt) {
LWWindowPeer winPeer = getWindowPeerOrSelf();
if (winPeer != null && winPeer != this) {
// We need to register the DropTarget in the
// peer of the window ancestor of the component
winPeer.addDropTarget(dt);
} else {
synchronized (dropTargetLock) {
// 10-14-02 VL: Windows WComponentPeer would add (or remove) the drop target only
// if it's the first (or last) one for the component. Otherwise this call is a no-op.
if (++fNumDropTargets == 1) {
// Having a non-null drop target would be an error but let's check just in case:
if (fDropTarget != null)
System.err.println("CComponent.addDropTarget(): current drop target is non-null.");
// Create a new drop target:
fDropTarget = CDropTarget.createDropTarget(dt, target, this);
}
}
}
}
protected AbstractTreeTransferHandler(DNDTree tree, int action, boolean drawIcon) {
this.tree = tree;
drawImage = drawIcon;
if (!GraphicsEnvironment.getLocalGraphicsEnvironment().isHeadlessInstance())
{
dragSource = new DragSource();
dgr = dragSource.createDefaultDragGestureRecognizer(tree, action, this);
dropTarget = new DropTarget(tree, action, this);
}
else
{
dragSource = null;
dgr = null;
dropTarget = null;
}
}
@Override
public void addDropTarget(DropTarget dt) {
LWWindowPeer winPeer = getWindowPeerOrSelf();
if (winPeer != null && winPeer != this) {
// We need to register the DropTarget in the
// peer of the window ancestor of the component
winPeer.addDropTarget(dt);
} else {
synchronized (dropTargetLock) {
// 10-14-02 VL: Windows WComponentPeer would add (or remove) the drop target only
// if it's the first (or last) one for the component. Otherwise this call is a no-op.
if (++fNumDropTargets == 1) {
// Having a non-null drop target would be an error but let's check just in case:
if (fDropTarget != null)
System.err.println("CComponent.addDropTarget(): current drop target is non-null.");
// Create a new drop target:
fDropTarget = CDropTarget.createDropTarget(dt, target, this);
}
}
}
}
@Override
public void removeDropTarget(DropTarget dt) {
LWWindowPeer winPeer = getWindowPeerOrSelf();
if (winPeer != null && winPeer != this) {
// We need to unregister the DropTarget in the
// peer of the window ancestor of the component
winPeer.removeDropTarget(dt);
} else {
synchronized (dropTargetLock){
// 10-14-02 VL: Windows WComponentPeer would add (or remove) the drop target only
// if it's the first (or last) one for the component. Otherwise this call is a no-op.
if (--fNumDropTargets == 0) {
// Having a null drop target would be an error but let's check just in case:
if (fDropTarget != null) {
// Dispose of the drop target:
fDropTarget.dispose();
fDropTarget = null;
} else
System.err.println("CComponent.removeDropTarget(): current drop target is null.");
}
}
}
}
@Override
public void removeDropTarget(DropTarget dt) {
LWWindowPeer winPeer = getWindowPeerOrSelf();
if (winPeer != null && winPeer != this) {
// We need to unregister the DropTarget in the
// peer of the window ancestor of the component
winPeer.removeDropTarget(dt);
} else {
synchronized (dropTargetLock){
// 10-14-02 VL: Windows WComponentPeer would add (or remove) the drop target only
// if it's the first (or last) one for the component. Otherwise this call is a no-op.
if (--fNumDropTargets == 0) {
// Having a null drop target would be an error but let's check just in case:
if (fDropTarget != null) {
// Dispose of the drop target:
fDropTarget.dispose();
fDropTarget = null;
} else
System.err.println("CComponent.removeDropTarget(): current drop target is null.");
}
}
}
}
private static void initAndShowUI() {
frame = new JFrame("Test frame");
frame.setSize(SIZE, SIZE);
frame.setLocationRelativeTo(null);
final JTextArea jta = new JTextArea();
jta.setBackground(Color.RED);
frame.add(jta);
jta.setText("1234567890");
jta.setFont(jta.getFont().deriveFont(150f));
jta.setDragEnabled(true);
jta.selectAll();
jta.setDropTarget(new DropTarget(jta, DnDConstants.ACTION_COPY,
new TestdropTargetListener()));
jta.addMouseListener(new TestMouseAdapter());
frame.setVisible(true);
}
@Override
public void removeDropTarget(DropTarget dt) {
LWWindowPeer winPeer = getWindowPeerOrSelf();
if (winPeer != null && winPeer != this) {
// We need to unregister the DropTarget in the
// peer of the window ancestor of the component
winPeer.removeDropTarget(dt);
} else {
synchronized (dropTargetLock){
// 10-14-02 VL: Windows WComponentPeer would add (or remove) the drop target only
// if it's the first (or last) one for the component. Otherwise this call is a no-op.
if (--fNumDropTargets == 0) {
// Having a null drop target would be an error but let's check just in case:
if (fDropTarget != null) {
// Dispose of the drop target:
fDropTarget.dispose();
fDropTarget = null;
} else
System.err.println("CComponent.removeDropTarget(): current drop target is null.");
}
}
}
}
private static void initAndShowUI() {
frame = new JFrame("Test frame");
frame.setSize(SIZE, SIZE);
frame.setLocationRelativeTo(null);
final JTextArea jta = new JTextArea();
jta.setBackground(Color.RED);
frame.add(jta);
jta.setText("1234567890");
jta.setFont(jta.getFont().deriveFont(150f));
jta.setDragEnabled(true);
jta.selectAll();
jta.setDropTarget(new DropTarget(jta, DnDConstants.ACTION_COPY,
new TestdropTargetListener()));
jta.addMouseListener(new TestMouseAdapter());
frame.setVisible(true);
}
private static void initAndShowUI() {
frame = new JFrame("Test frame");
frame.setSize(SIZE, SIZE);
frame.setLocationRelativeTo(null);
final JTextArea jta = new JTextArea();
jta.setBackground(Color.RED);
frame.add(jta);
jta.setText("1234567890");
jta.setFont(jta.getFont().deriveFont(150f));
jta.setDragEnabled(true);
jta.selectAll();
jta.setDropTarget(new DropTarget(jta, DnDConstants.ACTION_COPY,
new TestdropTargetListener()));
jta.addMouseListener(new TestMouseAdapter());
frame.setVisible(true);
}
private static void initAndShowUI() {
frame = new JFrame("Test frame");
frame.setSize(SIZE, SIZE);
frame.setLocationRelativeTo(null);
final JTextArea jta = new JTextArea();
jta.setBackground(Color.RED);
frame.add(jta);
jta.setText("1234567890");
jta.setFont(jta.getFont().deriveFont(150f));
jta.setDragEnabled(true);
jta.selectAll();
jta.setDropTarget(new DropTarget(jta, DnDConstants.ACTION_COPY,
new TestdropTargetListener()));
jta.addMouseListener(new TestMouseAdapter());
frame.setVisible(true);
}
/**
* Could be called when DnD enters a heavyweight or synthesized in processMotionMessage
*/
@Override
protected void processEnterMessage(SunDropTargetEvent event) {
Component c = event.getComponent();
DropTarget dt = event.getComponent().getDropTarget();
if (isEventInsideTarget(event)
&& dt != insideTarget
&& c.isShowing()
&& dt != null
&& dt.isActive()) {
insideTarget = dt;
super.processEnterMessage(event);
}
}
protected void installDragSupport() {
tabPane.setTransferHandler(TRANSFER_HANDLER);
try {
DropTarget target = tabPane.getDropTarget();
if (target != null) {
target.addDropTargetListener(TRANSFER_HANDLER);
target.setActive(dndEnabled);
}
} catch (TooManyListenersException e) {
e.printStackTrace();
}
}
public void removeDropTarget(DropTarget dt) {
Component comp = target;
while(!(comp == null || comp instanceof Window)) {
comp = comp.getParent();
}
if (comp instanceof Window) {
XWindowPeer wpeer = (XWindowPeer)(comp.getPeer());
if (wpeer != null) {
wpeer.removeDropTarget();
}
}
}
/**
* Constructs a new ruler for the specified graph and orientation.
*
* @param graph
* The graph to create the ruler for.
* @param orientation
* The orientation to use for the ruler.
*/
public EditorRuler(mxGraphComponent graphComponent, int orientation)
{
this.orientation = orientation;
this.graphComponent = graphComponent;
updateIncrementAndUnits();
graphComponent.getGraph().getView().addListener(
mxEvent.SCALE, repaintHandler);
graphComponent.getGraph().getView().addListener(
mxEvent.TRANSLATE, repaintHandler);
graphComponent.getGraph().getView().addListener(
mxEvent.SCALE_AND_TRANSLATE, repaintHandler);
graphComponent.getGraphControl().addMouseMotionListener(this);
DropTarget dropTarget = graphComponent.getDropTarget();
try
{
if (dropTarget != null)
{
dropTarget.addDropTargetListener(this);
}
}
catch (TooManyListenersException tmle)
{
// should not happen... swing drop target is multicast
}
setBorder(BorderFactory.createLineBorder(Color.black));
}
/****** DropTargetPeer implementation ********************/
public void addDropTarget(DropTarget dt) {
Component comp = target;
while(!(comp == null || comp instanceof Window)) {
comp = comp.getParent();
}
if (comp instanceof Window) {
XWindowPeer wpeer = (XWindowPeer)(comp.getPeer());
if (wpeer != null) {
wpeer.addDropTarget();
}
}
}
/**
* Installs a {@code DropTarget} on the component as necessary for a
* {@code TransferHandler} change.
*/
static void installSwingDropTargetAsNecessary(Component c,
TransferHandler t) {
if (!getSuppressDropTarget()) {
DropTarget dropHandler = c.getDropTarget();
if ((dropHandler == null) || (dropHandler instanceof UIResource)) {
if (t == null) {
c.setDropTarget(null);
} else if (!GraphicsEnvironment.isHeadless()) {
c.setDropTarget(new TransferHandler.SwingDropTarget(c));
}
}
}
}
/****** DropTargetPeer implementation ********************/
public void addDropTarget(DropTarget dt) {
Component comp = target;
while(!(comp == null || comp instanceof Window)) {
comp = comp.getParent();
}
if (comp instanceof Window) {
XWindowPeer wpeer = (XWindowPeer)(comp.getPeer());
if (wpeer != null) {
wpeer.addDropTarget();
}
}
}
/**
*
*/
private void dropHandle() {
// Handle drag & drop files into jPanel
jPanel_Device.setDropTarget(new DropTarget() {
@Override
public synchronized void drop(DropTargetDropEvent dtde) {
// disable UI
try {
// set accepted drop action
dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
// Just going to grab the expected DataFlavor to make sure
// we know what is being dropped
// Grab expected flavor
DataFlavor dragAndDropPanelFlavor = DataFlavor.javaFileListFlavor;
// What does the Transferable support
if (dtde.getTransferable().isDataFlavorSupported(dragAndDropPanelFlavor)) {
Object transferableArrayListObj = dtde.getTransferable().getTransferData(dragAndDropPanelFlavor);
if (transferableArrayListObj != null) {
if (transferableArrayListObj instanceof ArrayList) {
// copyHandle
List<MyFile> src = new ArrayList<>();
((ArrayList) transferableArrayListObj).forEach(file -> {
String srcFile = ((File) file).getAbsolutePath();
src.add(new MyFile(srcFile, 0, 0, 0));
});
// copy from local
fileBrowserLocal.onCopy(src);
// paste to device
fileBrowserDevice.onPaste();
}
}
}
} catch (UnsupportedFlavorException | IOException ex) {
Log.log(Level.SEVERE, "DropItHandle", ex);
}
// handle drop inside current panel
//super.drop(dtde);*/
}
});
}
/**
* unregister a DropTarget with this native peer
*/
@Override
public synchronized void removeDropTarget(DropTarget dt) {
nDropTargets--;
if (nDropTargets == 0) {
removeNativeDropTarget();
nativeDropTargetContext = 0;
}
}
private DropTargetContext createDropTargetContext() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
InstantiationException, IllegalAccessException, InvocationTargetException {
Constructor<DropTargetContext> c = DropTargetContext.class.getDeclaredConstructor(DropTarget.class);
c.setAccessible(true);
DropTargetContext inst = c.newInstance(dest.getDropTarget());
inst.addNotify(createDropTargetContextPeer());
return inst;
}
/**
* unregister a DropTarget with this native peer
*/
@Override
public synchronized void removeDropTarget(DropTarget dt) {
nDropTargets--;
if (nDropTargets == 0) {
removeNativeDropTarget();
nativeDropTargetContext = 0;
}
}
/**
* Instantiates a new style map table.
*
* @param model the model
* @param av the av
* @param edit the edit
* @param tmed the tmed
*/
public StyleMapTable(TableModel model, AnnotationFeaturesViewer av, StyleMapEditor edit,
TableGUIMediator tmed) {
super(model);
med = tmed;
setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
setDropTarget(new DropTarget(this, new TableDropAdapter(av, edit)));
getTableHeader().setReorderingAllowed(false);
ListSelectionModel lsm = this.getSelectionModel();
lsm.addListSelectionListener(new TableSelectionListener(med));
}
/**
* unregister a DropTarget with this native peer
*/
@Override
public synchronized void removeDropTarget(DropTarget dt) {
nDropTargets--;
if (nDropTargets == 0) {
removeNativeDropTarget();
nativeDropTargetContext = 0;
}
}
/** Creates the instance, makes given component active for
* drop operation. */
IndexedDropTarget(IndexedCustomizer dialog, IndexedDragSource ids) {
this.dialog = dialog;
this.comp = dialog.control;
this.cellRenderer = (IndexedListCellRenderer) this.comp.getCellRenderer();
this.ids = ids;
new DropTarget(comp, DnDConstants.ACTION_MOVE, this, true);
}
public static void drag()//定义的拖拽方法
{
//panel表示要接受拖拽的控件
new DropTarget(JlPath, DnDConstants.ACTION_COPY_OR_MOVE, new DropTargetAdapter() {
public void drop(DropTargetDropEvent dtde)//重写适配器的drop方法
{
try {
if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor))//如果拖入的文件格式受支持
{
dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);//接收拖拽来的数据
List<File> list = (List<File>) (dtde.getTransferable().getTransferData(DataFlavor.javaFileListFlavor));
String temp = "";
for (File file : list) {
temp = file.getAbsolutePath();
JlPath.setText(temp);
break;
}
//JOptionPane.showMessageDialog(null, temp);
dtde.dropComplete(true);//指示拖拽操作已完成
} else {
dtde.rejectDrop();//否则拒绝拖拽来的数据
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* unregister a DropTarget with this native peer
*/
@Override
public synchronized void removeDropTarget(DropTarget dt) {
nDropTargets--;
if (nDropTargets == 0) {
removeNativeDropTarget();
nativeDropTargetContext = 0;
}
}
void add( CategoryDescriptor descriptor ) {
CategoryList list = descriptor.getList();
list.setTransferHandler( null );
list.setDragEnabled(false);
recognizers.add( DragSource.getDefaultDragSource().createDefaultDragGestureRecognizer( list, DnDConstants.ACTION_MOVE, this ) );
dropTargets.add( new DropTarget( list, this ) );
CategoryButton button = descriptor.getButton();
button.setTransferHandler( null );
recognizers.add( DragSource.getDefaultDragSource().createDefaultDragGestureRecognizer( button, DnDConstants.ACTION_MOVE, this ) );
dropTargets.add( new DropTarget( button, this ) );
}
/**
* unregister a DropTarget with this native peer
*/
@Override
public synchronized void removeDropTarget(DropTarget dt) {
nDropTargets--;
if (nDropTargets == 0) {
removeNativeDropTarget();
nativeDropTargetContext = 0;
}
}
private MainPanel() {
super(new BorderLayout());
JTabbedPane tabs = new JTabbedPane();
tabs.addTab("00000000", new JScrollPane(makeList(0)));
tabs.addTab("11111111", new JScrollPane(makeList(1)));
tabs.addTab("22222222", new JScrollPane(makeList(2)));
add(tabs);
new DropTarget(tabs, DnDConstants.ACTION_MOVE, new TabTitleDropTargetListener(), true);
setPreferredSize(new Dimension(320, 240));
}
/**
* unregister a DropTarget with this native peer
*/
public synchronized void removeDropTarget(DropTarget dt) {
nDropTargets--;
if (nDropTargets == 0) {
removeNativeDropTarget();
nativeDropTargetContext = 0;
}
}