Movatterモバイル変換


[0]ホーム

URL:


Documentation

The Java™ Tutorials
Drag and Drop and Data Transfer
Introduction to DnD
Default DnD Support
Demo - BasicDnD
TransferHandler Class
Export Methods
Import Methods
TransferSupport Class
Setting the Drop Mode
Demo - DropDemo
Choosing the Drop Action
Demo - ChooseDropAction
Showing the Drop Location
Location Sensitive Drop
Demo - LocationSensitiveDemo
Empty Table Drop
Drop Location Rendering
Top-Level Drop
Adding Cut, Copy and Paste (CCP)
CCP in a Text Component
CCP in a non-Text Component
Using and Creating a DataFlavor
Putting it All Together - DnD and CCP
Solving Common Data Transfer Problems
Trail: Creating a GUI With Swing
Lesson: Drag and Drop and Data Transfer
Section: Location Sensitive Drop
Home Page >Creating a GUI With Swing >Drag and Drop and Data Transfer
« Previous • Trail • Next »

The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.
SeeDev.java for updated tutorials taking advantage of the latest releases.
SeeJava Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.
SeeJDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.

Demo - LocationSensitiveDemo

The following demo,LocationSensitiveDemo, shows aJTree that has been configured to support drop on any node except for one called "names" (or its descendants). Use the text field at the top of the frame as the drag source (it will automatically increment the string number each time you drag from there).

A combo box below the tree allows you to toggle the behavior for showing the drop location. Swing's default behavior is to show the drop location only when the area can accept the drop. You can override this behavior to always show the drop location (even if the area cannot accept the drop) or to never show the drop location (even if the area can accept the drop).

A snapshot of the LocationSensitiveDemo demo.

Try this: 
  1. Click the Launch button to runLocationSensitiveDemo usingJava™ Web Start (download JDK 7 or later). Alternatively, to compile and run the example yourself, consult theexample index.Launches the ListDemo example
  2. Initiate a drag by pressing on top of "String 0" in the text field and moving the mouse a short distance. Drag into the tree and move downwards. As you hover the mouse over most of the nodes, the drag acceptability is indicated by both the mouse cursor and by the node becoming highlighted. Drop the text onto the "colors" node. The new item becomes a child of that node and a sibling to the colors listed.
  3. Drag "String 1" from the textfield into the tree. Try to drop it on the "names" node. As you drag over that node or its children, Swing will not provide any drop location feedback and it will not accept the data.
  4. Change the "Show drop location" combo box to "Always".
  5. Repeat steps 1 and 2. The drop location now displays for the "names" node, but you cannot drop data into that area.
  6. Change the "Show drop location" combo box to "Never".
  7. Repeat steps 1 and 2. The drop location will not display for any part of the tree, though you can still drop data into the nodes, other than "names".

ThecanImport method forLocationSensitiveDemo looks like this:

public boolean canImport(TransferHandler.TransferSupport info) {    // for the demo, we will only support drops (not clipboard paste)    if (!info.isDrop()) {        return false;    }    String item = (String)indicateCombo.getSelectedItem();if (item.equals("Always")) {        info.setShowDropLocation(true);    } else if (item.equals("Never")) {        info.setShowDropLocation(false);    }    // we only import Strings    if (!info.isDataFlavorSupported(DataFlavor.stringFlavor)) {        return false;    }    // fetch the drop location    JTree.DropLocation dl = (JTree.DropLocation)info.getDropLocation();    TreePath path = dl.getPath();    // we do not support invalid paths or descendants of the names folderif (path == null || namesPath.isDescendant(path)) {        return false;    }    return true;}

The first code snippet displayed in bold modifies the drop location feedback mechanism. If "Always", then the drop location is always shown. If "Never", the drop location is never shown. Otherwise, the default behavior applies.

The second code snippet displayed in bold contains the logic that determines whether the tree will accept the data. If the path is not a valid path or if it is not the names path (or its descendant) it will return false and the import will not be accepted.

« PreviousTrailNext »

About Oracle |Contact Us |Legal Notices |Terms of Use |Your Privacy Rights

Copyright © 1995, 2024 Oracle and/or its affiliates. All rights reserved.

Previous page: Location Sensitive Drop
Next page: Empty Table Drop

[8]ページ先頭

©2009-2025 Movatter.jp