wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Drag & Drop images Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Drag & Drop images" Watch "Drag & Drop images" New topic
Author

Drag & Drop images

Hetal Seth
Greenhorn

Joined: Jun 18, 2001
Posts: 20
Hi All,
I have an application that does the DnD operation within a JTree. But, I want to implement the functionality of Dragging & Dropping an image (which, of course, would be a node of that same JTree).
Your assistance would be of great help.
Thanks,
Hetal Seth
Chantal Ackermann
Ranch Hand

Joined: Sep 28, 2000
Posts: 508
It's important to have a good TreeModel and custom TreeNodes that hold all the information concerning your objects in a straightforward manner. This way you get fast access to the objects and their attributes. If the model is good the rest drops in by itself. (hm, ok, most of the time, well, ...)
Hetal Seth
Greenhorn

Joined: Jun 18, 2001
Posts: 20
Hi,
I agree with you. But this is how far I got. I am not sure how to proceed further. Any hints.
Thanks,
Hetal Seth
*************************************************
import java.io.*;
import java.awt.*;
import java.net.*;
import java.lang.*;
import java.util.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
public class NodeManager implements TreeModel, TreeCellRenderer
{
static TreeModelListener l;
static GetPropObj getPropObj;
static Set setOfListeners = new HashSet();
DefaultMutableTreeNode dmtnScene;
NodeManager(GetPropObj getPropObj)
{
this.getPropObj = getPropObj;
NodeObjAvecChildren.setGetPropObj(getPropObj);
}
NodeManager(Socket socket, String logFileName) throws IOException
{
this.getPropObj = new GetPropObj(socket, logFileName);
NodeObjAvecChildren.setGetPropObj(getPropObj);
}
NodeManager(Socket socket) throws IOException
{
this.getPropObj = new GetPropObj(socket);
NodeObjAvecChildren.setGetPropObj(getPropObj);
}
public void addTreeModelListener(TreeModelListener l)
{
this.l = l;
setOfListeners.add(l);
}
public void removeTreeModelListener(TreeModelListener l)
{
setOfListeners.remove(l);
}
public void valueForPathChanged(TreePath path, Object newValue) { }
public Object getChild(Object parent, int index)
{
DefaultMutableTreeNode dmtnParent
= (DefaultMutableTreeNode)parent;
NodeObj nodeObjParent = getNodeObj(parent);
int targetChildCount = nodeObjParent.getChildCount();
DefaultMutableTreeNode dmtnChild;
/****
ASSERT(
defaultMutableTreeNode_Parent.getChildCount() == 0
||
defaultMutableTreeNode_Parent.getChildCount() == targetChildCount);
ASSERT(0 <= index && index = targetChildCount);
****/
if (dmtnParent.getChildCount() != targetChildCount) {
// safety: should be a no-op
dmtnParent.removeAllChildren();
for(int i = 0; i < targetChildCount; i++) {
NodeObj nodeObjChild
= nodeObjParent.getChild(i);
//XXX: Race condition
// children might be removed from the real
// object (on smanager) since we got the childcount.
dmtnChild = new DefaultMutableTreeNode(nodeObjChild);
dmtnParent.add(dmtnChild);
}
}
return dmtnParent.getChildAt(index);
}
public Object getRoot()
{
if(dmtnScene == null) {
NodeObj nodeScene = new NodeScene(getPropObj.getScene());
dmtnScene = new DefaultMutableTreeNode(nodeScene);
}
return dmtnScene;
}
public boolean isLeaf(Object node)
{
return getNodeObj(node).isLeaf();
}
public int getChildCount(Object parent)
{
return getNodeObj(parent).getChildCount();
}
public int getIndexOfChild(Object parent, Object child)
{
return (getNodeObj(parent)).getIndexOfChild(getNodeObj(child));
}
public Component getTreeCellRendererComponent(
JTreetree,
Objectvalue,
booleanselected,
booleanexpanded,
booleanleaf,
introw,
booleanhasFocus)
{
JLabel jlabel = new JLabel();
NodeObj nodeObj = getNodeObj(value);
if (selected) {
jlabel.setOpaque(true);
jlabel.setBackground(Color.lightGray);
}
jlabel.setIcon(nodeObj.getIcon());
jlabel.setText(nodeObj.getDname());
return jlabel;
}
static NodeObj getNodeObj(Object object)
{
DefaultMutableTreeNode dmtn
= (DefaultMutableTreeNode)object;
return (NodeObj)dmtn.getUserObject();
}
}
Chantal Ackermann
Ranch Hand

Joined: Sep 28, 2000
Posts: 508
Don't you have a custom TreeNode implementation? I experienced some problems in relying on DefaultMutableTreeNode: the TreeModel sometimes handles TreeNodes and sometimes DefaultMutableTreeNodes. so type casting might fail from time to time.
you have to listen to where the image is dropped in the tree and compute from this point where to insert it in the tree. then you have to create a new tree node with your image as (part of) the user object. (you won't display the image in the tree, will you? I imagine it is to big?!)
To listen to drag events you need DragGestureRecognizer:

the listener will then call the method that computes the drop point and inserts the new node if the image gets dropped.
cheers
Hetal Seth
Greenhorn

Joined: Jun 18, 2001
Posts: 20
Hi,
The 'NodeObj' is my custom TreeNode. I guess I have put forth my question a bit wrong. As you can notice from the previous source code, I have a tree in which each treenode is a label comprising of an image + text. When I drag that node, I want a GHOSTLY image of that treenode(either text+image together or only image) to move along the cursor to the drop point, like the way it happens in windows explorer. My drag & drop implementation is working good presently without the above mentioned feature.
I have 3 different kinds of images that are red, green, blue color for different purposes. There are 2 possibilities.
1. Create a grey image replicating each of the above mentioned images & then call upon the 'grey' image in the dragEnter() of DRAG till drop() of DROP, I mean something like that.
-> OR <-
2. Write a code which would create that ghostly image on the fly with the help of BufferedImage, Graphics2D, etc. & suppy it to startDrag() in the dragGestureRecognised().
Implementation wise (2) is a little tricky, which i dont know how to do. But logically & functionality-wise, (2) is the right thing to do, I guess.
Let me know if I am thinking in the right direction.
Thanks,
Hetal Seh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Drag & Drop images