-
Bug
-
Resolution: Fixed
-
P3
-
1.2.0, 1.3.0, 1.4.0
-
beta2
-
generic, x86, sparc
-
generic, solaris_2.6, windows_nt, windows_2000
dragSource.createDefaultDragGestureRecognizer( this,
DnDConstants.ACTION_MOVE, this );
}
public void dragDropEnd(DragSourceDropEvent e) {}
public void dragEnter(DragSourceDragEvent e) {}
public void dragExit(DragSourceEvent e) {}
public void dragOver(DragSourceDragEvent e) {}
public void dropActionChanged(DragSourceDragEvent e) {}
public void dragGestureRecognized(DragGestureEvent e)
{
System.out.println(" in dragGestureRecognized");
e.startDrag(null, new StringSelection("hello"), this);
}
public void dragEnter(DropTargetDragEvent dtde) { }
public void dragExit(DropTargetEvent dte) { }
public void dropActionChanged(DropTargetDragEvent dtde){}
public void dragOver(DropTargetDragEvent dtde) { }
public void drop(DropTargetDropEvent dtde) { }
}
public TreeTest()
{
super(new BorderLayout());
JScrollPane sp = new JScrollPane(new MyTree());
add("Center", sp);
}
public static void main(String[] args)
{
JFrame frame = new JFrame("JTree Test");
frame.getContentPane().add("Center", new TreeTest());
frame.pack();
frame.setVisible(true);
}
}
///////////////////////////////////////////////////////////////////
(Review ID: 111290)
======================================================================
Name: krC82822 Date: 12/09/2000
9 Dec 2000, eval1127@eng -- reproducible with merlin-beta and 1.3.0
on Solaris and win32.
User's example employs java.awt.datatransfer.DataFlavor.PlainTextFlavor,
which has been deprecated.
See also bug #4273712.
---------------
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0beta-b44) [Solaris]
Java HotSpot(TM) Client VM (build 1.4beta-B44, mixed mode)
java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0)
Java HotSpot(TM) Client VM (build 1.3.0, mixed mode)
(plus 1.4beta-b43 and 1.3.0-C on NT 4.0 SP6a)
I have a JTree which does not show the whole hierarchy.
I resize the tree so that the last line is at the bottom of the window, and the
horizontal scrollbar is not visible.
I develop the hierarchy of the last item: the tree adds the horizontal scroll
bar, and scrolls so that the last line is visible.
The result of this manipulation is that the Drag and Drop starts automatically.
Here is a sample code:
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.dnd.*;
import java.io.*;
import java.util.Iterator;
import java.util.Hashtable;
import java.util.List;
import java.awt.event.*;
import java.awt.datatransfer.*;
public class TreeTester {
public static void main (String args[]) {
JFrame f = new JFrame("Tree Dragging Tester");
CustomCellRenderer renderer =
new CustomCellRenderer();
DraggableTree tree = new DraggableTree();
tree.setModel(getDefaultTreeModel());
tree.setCellRenderer(renderer);
JScrollPane leftPane = new JScrollPane(tree);
DroppableList list = new DroppableList();
list.setCellRenderer(renderer);
JScrollPane rightPane = new JScrollPane(list);
JSplitPane splitPane =
new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPane, rightPane);
f.getContentPane().add (splitPane,
BorderLayout.CENTER);
f.setSize (400, 300);
f.addWindowListener (new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.setVisible (true);
}
private static TreeModel getDefaultTreeModel() {
DefaultMutableTreeNode root =
new DefaultMutableTreeNode("Drag Me");
DefaultMutableTreeNode parent;
parent = new DefaultMutableTreeNode("Auctions");
root.add(parent);
parent.add(new DefaultMutableTreeNode
(makeNode("eBay", "http://www.ebay.com")));
parent.add(new DefaultMutableTreeNode
(makeNode("EggHead", "http://www.egghead.com")));
parent.add(new DefaultMutableTreeNode
(makeNode("First Auction", "http://www.firstauction.com")));
parent.add(new DefaultMutableTreeNode
(makeNode("uBid", "http://www.ubid.com")));
parent = new
DefaultMutableTreeNode("Search Engines");
root.add(parent);
parent.add(new DefaultMutableTreeNode
(makeNode("HotBot", "http://www.hotbot.com")));
parent.add(new DefaultMutableTreeNode
(makeNode("Infoseek", "http://www.infoseek.com")));
parent.add(new DefaultMutableTreeNode
(makeNode("Lycos", "http://www.lycos.com")));
parent.add(new DefaultMutableTreeNode
(makeNode("Yahoo", "http://www.yahoo.com")));
parent = new DefaultMutableTreeNode("Java");
root.add(parent);
parent.add(new DefaultMutableTreeNode
(makeNode("Focus on Java", "http://java.about.com")));
parent.add(new DefaultMutableTreeNode
(makeNode("JavaWorld", "http://www.javaworld.com")));
parent.add(new DefaultMutableTreeNode
(makeNode("Sun", "http://java.sun.com")));
return new DefaultTreeModel(root);
}
private static Hashtable makeNode(String name,
String url) {
Hashtable hashtable = new Hashtable();
hashtable.put("name", name);
hashtable.put("url", url);
return hashtable;
}
}
class DraggableTree extends JTree
implements DragGestureListener {
DragSource dragSource =
DragSource.getDefaultDragSource();
final static DragSourceListener dragSourceListener
= new MyDragSourceListener();
static class MyDragSourceListener
implements DragSourceListener {
public void dragDropEnd(DragSourceDropEvent DragSourceDropEvent) {
}
public void dragEnter(DragSourceDragEvent DragSourceDragEvent) {
}
public void dragExit(DragSourceEvent DragSourceEvent) {
}
public void dragOver(DragSourceDragEvent DragSourceDragEvent) {
}
public void dropActionChanged(DragSourceDragEvent
DragSourceDragEvent) {
}
}
public DraggableTree () {
dragSource.createDefaultDragGestureRecognizer
(this, DnDConstants.ACTION_COPY_OR_MOVE, this);
}
public DraggableTree (TreeModel model) {
super (model);
dragSource.createDefaultDragGestureRecognizer
(this, DnDConstants.ACTION_COPY_OR_MOVE, this);
}
// DragGestureListener
public void dragGestureRecognized(
DragGestureEvent dragGestureEvent) {
TreePath path = getSelectionPath();
if (path == null) {
// Nothing selected, nothing to drag
System.out.println ("Nothing selected - beep");
getToolkit().beep();
} else {
DefaultMutableTreeNode selection =
(DefaultMutableTreeNode)
path.getLastPathComponent();
TransferableTreeNode node =
new TransferableTreeNode(selection);
dragSource.startDrag(
dragGestureEvent,
DragSource.DefaultCopyDrop,
node,
dragSourceListener);
}
}
}
class TransferableTreeNode
extends DefaultMutableTreeNode
implements Transferable {
final static int TREE = 0;
final static int STRING = 1;
final static int PLAIN_TEXT = 2;
final public static DataFlavor
DEFAULT_MUTABLE_TREENODE_FLAVOR =
new DataFlavor(
DefaultMutableTreeNode.class,
"Default Mutable Tree Node");
static DataFlavor flavors[] = {
DEFAULT_MUTABLE_TREENODE_FLAVOR,
DataFlavor.stringFlavor,
DataFlavor.plainTextFlavor};
/* The following works fine
static DataFlavor flavors[] = {
DEFAULT_MUTABLE_TREENODE_FLAVOR
};
*/
private DefaultMutableTreeNode data;
public TransferableTreeNode(
DefaultMutableTreeNode data) {
this.data = data;
}
public DataFlavor[] getTransferDataFlavors() {
return flavors;
}
public Object getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException,
IOException {
Object returnObject;
if (flavor.equals(flavors[TREE])) {
Object userObject = data.getUserObject();
if (userObject == null) {
returnObject = data;
} else {
returnObject = userObject;
}
} else if (flavor.equals(flavors[STRING])) {
Object userObject = data.getUserObject();
if (userObject == null) {
returnObject = data.toString();
} else {
returnObject = userObject.toString();
}
} else if (flavor.equals(flavors[PLAIN_TEXT])) {
Object userObject = data.getUserObject();
String string;
if (userObject == null) {
string = data.toString();
} else {
string = userObject.toString();
}
returnObject = new ByteArrayInputStream(
string.getBytes());
} else {
throw new UnsupportedFlavorException(flavor);
}
return returnObject;
}
public boolean isDataFlavorSupported(
DataFlavor flavor) {
boolean returnValue = false;
for (int i=0, n=flavors.length; i<n; i++) {
if (flavor.equals(flavors[i])) {
returnValue = true;
break;
}
}
return returnValue;
}
}
class DroppableList extends JList
implements DropTargetListener {
DropTarget dropTarget;
public DroppableList() {
dropTarget = new DropTarget (this, this);
setModel(new DefaultListModel());
addMouseListener
(
new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
JPopupMenu pm = new JPopupMenu("hello");
pm.add(new JMenuItem
("menuitem xxxxxxxxxxxxxxxxxxxxxx 1"));
pm.add(new JMenuItem
("menuitem xxxxxxxxxxxxxxxxxxxxxx 2"));
pm.show(DroppableList.this, e.getX(), e.getY());
}
}
);
}
public void dragEnter(DropTargetDragEvent dropTargetDragEvent) {
dropTargetDragEvent.acceptDrag (
DnDConstants.ACTION_COPY_OR_MOVE);
}
public void dragExit(DropTargetEvent dropTargetEvent) {
}
public void dragOver(DropTargetDragEvent dropTargetDragEvent) {
}
public void dropActionChanged(DropTargetDragEvent dropTargetDragEvent) {
}
public synchronized void drop(DropTargetDropEvent dropTargetDropEvent) {
try {
Transferable tr =
dropTargetDropEvent.getTransferable();
if (tr.isDataFlavorSupported(
TransferableTreeNode.
DEFAULT_MUTABLE_TREENODE_FLAVOR)) {
dropTargetDropEvent.acceptDrop
(DnDConstants.ACTION_COPY_OR_MOVE);
Object userObject = tr.getTransferData
(TransferableTreeNode.DEFAULT_MUTABLE_TREENODE_FLAVOR);
((DefaultListModel)getModel()).
addElement(userObject);
dropTargetDropEvent.getDropTargetContext().
dropComplete(true);
} else if (tr.isDataFlavorSupported(DataFlavor.stringFlavor)) {
dropTargetDropEvent.acceptDrop
(DnDConstants.ACTION_COPY_OR_MOVE);
String string = (String)tr.getTransferData
(DataFlavor.stringFlavor);
((DefaultListModel)getModel()).addElement(string);
dropTargetDropEvent.getDropTargetContext().
dropComplete(true);
} else if (tr.isDataFlavorSupported(DataFlavor.plainTextFlavor)) {
dropTargetDropEvent.acceptDrop
(DnDConstants.ACTION_COPY_OR_MOVE);
Object stream = tr.getTransferData(DataFlavor.plainTextFlavor);
if (stream instanceof InputStream) {
InputStreamReader isr =
new InputStreamReader((InputStream)stream);
BufferedReader reader =
new BufferedReader(isr);
String line;
while ((line = reader.readLine()) != null) {
((DefaultListModel)getModel()).
addElement(line);
}
dropTargetDropEvent.getDropTargetContext().
dropComplete(true);
} else if (stream instanceof Reader) {
BufferedReader reader =
new BufferedReader((Reader)stream);
String line;
while ((line = reader.readLine()) != null) {
((DefaultListModel)getModel()).
addElement(line);
}
dropTargetDropEvent.getDropTargetContext().
dropComplete(true);
} else {
System.err.println ("Unknown type: " +
stream.getClass());
dropTargetDropEvent.rejectDrop();
}
} else if (tr.isDataFlavorSupported
(DataFlavor.javaFileListFlavor)) {
dropTargetDropEvent.acceptDrop
(DnDConstants.ACTION_COPY_OR_MOVE);
List fileList = (List)tr.getTransferData
(DataFlavor.javaFileListFlavor);
Iterator iterator = fileList.iterator();
while (iterator.hasNext()) {
File file = (File)iterator.next();
Hashtable hashtable = new Hashtable();
hashtable.put("name", file.getName());
hashtable.put("url",
file.toURL().toString());
((DefaultListModel)getModel()).
addElement(hashtable);
}
dropTargetDropEvent.getDropTargetContext().
dropComplete(true);
} else {
System.err.println ("Rejected");
dropTargetDropEvent.rejectDrop();
}
} catch (IOException io) {
io.printStackTrace();
dropTargetDropEvent.rejectDrop();
} catch (UnsupportedFlavorException ufe) {
ufe.printStackTrace();
dropTargetDropEvent.rejectDrop();
}
}
}
class CustomCellRenderer
implements ListCellRenderer, TreeCellRenderer {
DefaultListCellRenderer listCellRenderer =
new DefaultListCellRenderer();
DefaultTreeCellRenderer treeCellRenderer =
new DefaultTreeCellRenderer();
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean selected,
boolean hasFocus) {
listCellRenderer.getListCellRendererComponent(list, value, index,
selected, hasFocus);
listCellRenderer.setText(getValueString(value));
return listCellRenderer;
}
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean selected,
boolean expanded,
boolean leaf, int row,
boolean hasFocus) {
treeCellRenderer.getTreeCellRendererComponent(tree, value, selected,
expanded, leaf, row,
hasFocus);
if (value instanceof DefaultMutableTreeNode) {
DefaultMutableTreeNode node =
(DefaultMutableTreeNode)value;
value = node.getUserObject();
}
treeCellRenderer.setText(getValueString(value));
return treeCellRenderer;
}
private String getValueString(Object value) {
String returnString = "null";
if (value != null) {
if (value instanceof Hashtable) {
Hashtable h = (Hashtable)value;
String name = (String)h.get("name");
String url = (String)h.get("url");
returnString = name + " ==> " + url;
} else {
returnString = "X: " + value.toString();
}
}
return returnString;
}
}
(Review ID: 113124)
======================================================================
Name: krC82822 Date: 12/09/2000
>> java -version
java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)
A JTree which is setup to support drag and drop and placed in a
JScrollPane can end up calling dragGestureRecognized if you
expand a node in the tree such that the bottom scrollbar
will appear under where your mouse is.
This problem happens on both NT and Solaris.
On Solaris I also get this error:
Exception in thread "AWT-Motif" java.lang.UnsatisfiedLinkError: findComponentAt
at sun.awt.motif.MDropTargetContextPeer.findComponentAt(Native Method)
at
sun.awt.motif.MDropTargetContextPeer.handleEnterMessage(MDropTargetContextPeer.j
ava:408)
at sun.awt.motif.MToolkit.run(Native Method)
at java.land.Thread.run(Thread.java 484)
Here's a small test program to illustrate the problem.
////////////////////////////////////////////////////////////////
// Resize the Frame horizontally and vertically so that the frame is just
// big enough to show the items in the tree. The food tree node
// should be just above the bottom of the screen. Make sure it
// is big enough so that niether of the scrollbars are visible, but not
// any bigger.
// Either double click on the bottom part of the food item to expand it,
// or single click on the bottom part of the food turner to expand it.
// When the node expands, the horizontal scrollbar should be over your mouse
// pointer. You should also see that the dragGestureRecognized method was
// called, and the mouse cursor changed to the drag icon and will not
// change back until you click again.
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.dnd.*;
import java.awt.datatransfer.*;
public class TreeTest extends JPanel
{
public class MyTree extends JTree implements DragGestureListener,
DragSourceListener, DropTargetListener
{
protected DragSource dragSource = DragSource.getDefaultDragSource();
protected DropTarget dropTarget = null;
public MyTree()
{
super();
dropTarget = new DropTarget (this, this);
DnDConstants.ACTION_MOVE, this );
}
public void dragDropEnd(DragSourceDropEvent e) {}
public void dragEnter(DragSourceDragEvent e) {}
public void dragExit(DragSourceEvent e) {}
public void dragOver(DragSourceDragEvent e) {}
public void dropActionChanged(DragSourceDragEvent e) {}
public void dragGestureRecognized(DragGestureEvent e)
{
System.out.println(" in dragGestureRecognized");
e.startDrag(null, new StringSelection("hello"), this);
}
public void dragEnter(DropTargetDragEvent dtde) { }
public void dragExit(DropTargetEvent dte) { }
public void dropActionChanged(DropTargetDragEvent dtde){}
public void dragOver(DropTargetDragEvent dtde) { }
public void drop(DropTargetDropEvent dtde) { }
}
public TreeTest()
{
super(new BorderLayout());
JScrollPane sp = new JScrollPane(new MyTree());
add("Center", sp);
}
public static void main(String[] args)
{
JFrame frame = new JFrame("JTree Test");
frame.getContentPane().add("Center", new TreeTest());
frame.pack();
frame.setVisible(true);
}
}
///////////////////////////////////////////////////////////////////
(Review ID: 111290)
======================================================================
Name: krC82822 Date: 12/09/2000
9 Dec 2000, eval1127@eng -- reproducible with merlin-beta and 1.3.0
on Solaris and win32.
User's example employs java.awt.datatransfer.DataFlavor.PlainTextFlavor,
which has been deprecated.
See also bug #4273712.
---------------
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0beta-b44) [Solaris]
Java HotSpot(TM) Client VM (build 1.4beta-B44, mixed mode)
java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0)
Java HotSpot(TM) Client VM (build 1.3.0, mixed mode)
(plus 1.4beta-b43 and 1.3.0-C on NT 4.0 SP6a)
I have a JTree which does not show the whole hierarchy.
I resize the tree so that the last line is at the bottom of the window, and the
horizontal scrollbar is not visible.
I develop the hierarchy of the last item: the tree adds the horizontal scroll
bar, and scrolls so that the last line is visible.
The result of this manipulation is that the Drag and Drop starts automatically.
Here is a sample code:
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.dnd.*;
import java.io.*;
import java.util.Iterator;
import java.util.Hashtable;
import java.util.List;
import java.awt.event.*;
import java.awt.datatransfer.*;
public class TreeTester {
public static void main (String args[]) {
JFrame f = new JFrame("Tree Dragging Tester");
CustomCellRenderer renderer =
new CustomCellRenderer();
DraggableTree tree = new DraggableTree();
tree.setModel(getDefaultTreeModel());
tree.setCellRenderer(renderer);
JScrollPane leftPane = new JScrollPane(tree);
DroppableList list = new DroppableList();
list.setCellRenderer(renderer);
JScrollPane rightPane = new JScrollPane(list);
JSplitPane splitPane =
new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPane, rightPane);
f.getContentPane().add (splitPane,
BorderLayout.CENTER);
f.setSize (400, 300);
f.addWindowListener (new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.setVisible (true);
}
private static TreeModel getDefaultTreeModel() {
DefaultMutableTreeNode root =
new DefaultMutableTreeNode("Drag Me");
DefaultMutableTreeNode parent;
parent = new DefaultMutableTreeNode("Auctions");
root.add(parent);
parent.add(new DefaultMutableTreeNode
(makeNode("eBay", "http://www.ebay.com")));
parent.add(new DefaultMutableTreeNode
(makeNode("EggHead", "http://www.egghead.com")));
parent.add(new DefaultMutableTreeNode
(makeNode("First Auction", "http://www.firstauction.com")));
parent.add(new DefaultMutableTreeNode
(makeNode("uBid", "http://www.ubid.com")));
parent = new
DefaultMutableTreeNode("Search Engines");
root.add(parent);
parent.add(new DefaultMutableTreeNode
(makeNode("HotBot", "http://www.hotbot.com")));
parent.add(new DefaultMutableTreeNode
(makeNode("Infoseek", "http://www.infoseek.com")));
parent.add(new DefaultMutableTreeNode
(makeNode("Lycos", "http://www.lycos.com")));
parent.add(new DefaultMutableTreeNode
(makeNode("Yahoo", "http://www.yahoo.com")));
parent = new DefaultMutableTreeNode("Java");
root.add(parent);
parent.add(new DefaultMutableTreeNode
(makeNode("Focus on Java", "http://java.about.com")));
parent.add(new DefaultMutableTreeNode
(makeNode("JavaWorld", "http://www.javaworld.com")));
parent.add(new DefaultMutableTreeNode
(makeNode("Sun", "http://java.sun.com")));
return new DefaultTreeModel(root);
}
private static Hashtable makeNode(String name,
String url) {
Hashtable hashtable = new Hashtable();
hashtable.put("name", name);
hashtable.put("url", url);
return hashtable;
}
}
class DraggableTree extends JTree
implements DragGestureListener {
DragSource dragSource =
DragSource.getDefaultDragSource();
final static DragSourceListener dragSourceListener
= new MyDragSourceListener();
static class MyDragSourceListener
implements DragSourceListener {
public void dragDropEnd(DragSourceDropEvent DragSourceDropEvent) {
}
public void dragEnter(DragSourceDragEvent DragSourceDragEvent) {
}
public void dragExit(DragSourceEvent DragSourceEvent) {
}
public void dragOver(DragSourceDragEvent DragSourceDragEvent) {
}
public void dropActionChanged(DragSourceDragEvent
DragSourceDragEvent) {
}
}
public DraggableTree () {
dragSource.createDefaultDragGestureRecognizer
(this, DnDConstants.ACTION_COPY_OR_MOVE, this);
}
public DraggableTree (TreeModel model) {
super (model);
dragSource.createDefaultDragGestureRecognizer
(this, DnDConstants.ACTION_COPY_OR_MOVE, this);
}
// DragGestureListener
public void dragGestureRecognized(
DragGestureEvent dragGestureEvent) {
TreePath path = getSelectionPath();
if (path == null) {
// Nothing selected, nothing to drag
System.out.println ("Nothing selected - beep");
getToolkit().beep();
} else {
DefaultMutableTreeNode selection =
(DefaultMutableTreeNode)
path.getLastPathComponent();
TransferableTreeNode node =
new TransferableTreeNode(selection);
dragSource.startDrag(
dragGestureEvent,
DragSource.DefaultCopyDrop,
node,
dragSourceListener);
}
}
}
class TransferableTreeNode
extends DefaultMutableTreeNode
implements Transferable {
final static int TREE = 0;
final static int STRING = 1;
final static int PLAIN_TEXT = 2;
final public static DataFlavor
DEFAULT_MUTABLE_TREENODE_FLAVOR =
new DataFlavor(
DefaultMutableTreeNode.class,
"Default Mutable Tree Node");
static DataFlavor flavors[] = {
DEFAULT_MUTABLE_TREENODE_FLAVOR,
DataFlavor.stringFlavor,
DataFlavor.plainTextFlavor};
/* The following works fine
static DataFlavor flavors[] = {
DEFAULT_MUTABLE_TREENODE_FLAVOR
};
*/
private DefaultMutableTreeNode data;
public TransferableTreeNode(
DefaultMutableTreeNode data) {
this.data = data;
}
public DataFlavor[] getTransferDataFlavors() {
return flavors;
}
public Object getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException,
IOException {
Object returnObject;
if (flavor.equals(flavors[TREE])) {
Object userObject = data.getUserObject();
if (userObject == null) {
returnObject = data;
} else {
returnObject = userObject;
}
} else if (flavor.equals(flavors[STRING])) {
Object userObject = data.getUserObject();
if (userObject == null) {
returnObject = data.toString();
} else {
returnObject = userObject.toString();
}
} else if (flavor.equals(flavors[PLAIN_TEXT])) {
Object userObject = data.getUserObject();
String string;
if (userObject == null) {
string = data.toString();
} else {
string = userObject.toString();
}
returnObject = new ByteArrayInputStream(
string.getBytes());
} else {
throw new UnsupportedFlavorException(flavor);
}
return returnObject;
}
public boolean isDataFlavorSupported(
DataFlavor flavor) {
boolean returnValue = false;
for (int i=0, n=flavors.length; i<n; i++) {
if (flavor.equals(flavors[i])) {
returnValue = true;
break;
}
}
return returnValue;
}
}
class DroppableList extends JList
implements DropTargetListener {
DropTarget dropTarget;
public DroppableList() {
dropTarget = new DropTarget (this, this);
setModel(new DefaultListModel());
addMouseListener
(
new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
JPopupMenu pm = new JPopupMenu("hello");
pm.add(new JMenuItem
("menuitem xxxxxxxxxxxxxxxxxxxxxx 1"));
pm.add(new JMenuItem
("menuitem xxxxxxxxxxxxxxxxxxxxxx 2"));
pm.show(DroppableList.this, e.getX(), e.getY());
}
}
);
}
public void dragEnter(DropTargetDragEvent dropTargetDragEvent) {
dropTargetDragEvent.acceptDrag (
DnDConstants.ACTION_COPY_OR_MOVE);
}
public void dragExit(DropTargetEvent dropTargetEvent) {
}
public void dragOver(DropTargetDragEvent dropTargetDragEvent) {
}
public void dropActionChanged(DropTargetDragEvent dropTargetDragEvent) {
}
public synchronized void drop(DropTargetDropEvent dropTargetDropEvent) {
try {
Transferable tr =
dropTargetDropEvent.getTransferable();
if (tr.isDataFlavorSupported(
TransferableTreeNode.
DEFAULT_MUTABLE_TREENODE_FLAVOR)) {
dropTargetDropEvent.acceptDrop
(DnDConstants.ACTION_COPY_OR_MOVE);
Object userObject = tr.getTransferData
(TransferableTreeNode.DEFAULT_MUTABLE_TREENODE_FLAVOR);
((DefaultListModel)getModel()).
addElement(userObject);
dropTargetDropEvent.getDropTargetContext().
dropComplete(true);
} else if (tr.isDataFlavorSupported(DataFlavor.stringFlavor)) {
dropTargetDropEvent.acceptDrop
(DnDConstants.ACTION_COPY_OR_MOVE);
String string = (String)tr.getTransferData
(DataFlavor.stringFlavor);
((DefaultListModel)getModel()).addElement(string);
dropTargetDropEvent.getDropTargetContext().
dropComplete(true);
} else if (tr.isDataFlavorSupported(DataFlavor.plainTextFlavor)) {
dropTargetDropEvent.acceptDrop
(DnDConstants.ACTION_COPY_OR_MOVE);
Object stream = tr.getTransferData(DataFlavor.plainTextFlavor);
if (stream instanceof InputStream) {
InputStreamReader isr =
new InputStreamReader((InputStream)stream);
BufferedReader reader =
new BufferedReader(isr);
String line;
while ((line = reader.readLine()) != null) {
((DefaultListModel)getModel()).
addElement(line);
}
dropTargetDropEvent.getDropTargetContext().
dropComplete(true);
} else if (stream instanceof Reader) {
BufferedReader reader =
new BufferedReader((Reader)stream);
String line;
while ((line = reader.readLine()) != null) {
((DefaultListModel)getModel()).
addElement(line);
}
dropTargetDropEvent.getDropTargetContext().
dropComplete(true);
} else {
System.err.println ("Unknown type: " +
stream.getClass());
dropTargetDropEvent.rejectDrop();
}
} else if (tr.isDataFlavorSupported
(DataFlavor.javaFileListFlavor)) {
dropTargetDropEvent.acceptDrop
(DnDConstants.ACTION_COPY_OR_MOVE);
List fileList = (List)tr.getTransferData
(DataFlavor.javaFileListFlavor);
Iterator iterator = fileList.iterator();
while (iterator.hasNext()) {
File file = (File)iterator.next();
Hashtable hashtable = new Hashtable();
hashtable.put("name", file.getName());
hashtable.put("url",
file.toURL().toString());
((DefaultListModel)getModel()).
addElement(hashtable);
}
dropTargetDropEvent.getDropTargetContext().
dropComplete(true);
} else {
System.err.println ("Rejected");
dropTargetDropEvent.rejectDrop();
}
} catch (IOException io) {
io.printStackTrace();
dropTargetDropEvent.rejectDrop();
} catch (UnsupportedFlavorException ufe) {
ufe.printStackTrace();
dropTargetDropEvent.rejectDrop();
}
}
}
class CustomCellRenderer
implements ListCellRenderer, TreeCellRenderer {
DefaultListCellRenderer listCellRenderer =
new DefaultListCellRenderer();
DefaultTreeCellRenderer treeCellRenderer =
new DefaultTreeCellRenderer();
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean selected,
boolean hasFocus) {
listCellRenderer.getListCellRendererComponent(list, value, index,
selected, hasFocus);
listCellRenderer.setText(getValueString(value));
return listCellRenderer;
}
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean selected,
boolean expanded,
boolean leaf, int row,
boolean hasFocus) {
treeCellRenderer.getTreeCellRendererComponent(tree, value, selected,
expanded, leaf, row,
hasFocus);
if (value instanceof DefaultMutableTreeNode) {
DefaultMutableTreeNode node =
(DefaultMutableTreeNode)value;
value = node.getUserObject();
}
treeCellRenderer.setText(getValueString(value));
return treeCellRenderer;
}
private String getValueString(Object value) {
String returnString = "null";
if (value != null) {
if (value instanceof Hashtable) {
Hashtable h = (Hashtable)value;
String name = (String)h.get("name");
String url = (String)h.get("url");
returnString = name + " ==> " + url;
} else {
returnString = "X: " + value.toString();
}
}
return returnString;
}
}
(Review ID: 113124)
======================================================================
Name: krC82822 Date: 12/09/2000
>> java -version
java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)
A JTree which is setup to support drag and drop and placed in a
JScrollPane can end up calling dragGestureRecognized if you
expand a node in the tree such that the bottom scrollbar
will appear under where your mouse is.
This problem happens on both NT and Solaris.
On Solaris I also get this error:
Exception in thread "AWT-Motif" java.lang.UnsatisfiedLinkError: findComponentAt
at sun.awt.motif.MDropTargetContextPeer.findComponentAt(Native Method)
at
sun.awt.motif.MDropTargetContextPeer.handleEnterMessage(MDropTargetContextPeer.j
ava:408)
at sun.awt.motif.MToolkit.run(Native Method)
at java.land.Thread.run(Thread.java 484)
Here's a small test program to illustrate the problem.
////////////////////////////////////////////////////////////////
// Resize the Frame horizontally and vertically so that the frame is just
// big enough to show the items in the tree. The food tree node
// should be just above the bottom of the screen. Make sure it
// is big enough so that niether of the scrollbars are visible, but not
// any bigger.
// Either double click on the bottom part of the food item to expand it,
// or single click on the bottom part of the food turner to expand it.
// When the node expands, the horizontal scrollbar should be over your mouse
// pointer. You should also see that the dragGestureRecognized method was
// called, and the mouse cursor changed to the drag icon and will not
// change back until you click again.
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.dnd.*;
import java.awt.datatransfer.*;
public class TreeTest extends JPanel
{
public class MyTree extends JTree implements DragGestureListener,
DragSourceListener, DropTargetListener
{
protected DragSource dragSource = DragSource.getDefaultDragSource();
protected DropTarget dropTarget = null;
public MyTree()
{
super();
dropTarget = new DropTarget (this, this);
- duplicates
-
JDK-4337114 Drag and Drop does not work correctly when JInternalFrame selection changes
-
- Closed
-
- relates to
-
JDK-4354044 Hotspot EXCEPTION_ACCESS_VIOLATION using invokeLater on a DragGestureEvent
-
- Resolved
-
-
JDK-4273712 Triple click on editable JTree causes dragGestureRecognized to be called
-
- Resolved
-