Unable to drag the Icon associated with the node

I am working on dragging a node from Tree to a list.
I have to drag the Icon associated with the leaf too. I am able to drag only the string of the node not the Icon that is associated with it. Can you help me with this? I have the code as below. Thanks.
public class IconNode extends DefaultMutableTreeNode {
protected Icon icon;
protected String iconName;
//* IconNode
/**Creates an IconNode Object*/
public IconNode() {
this(null);
}//end IconNode
//* IconNode
/**Creates an IconNode Object*/
public IconNode(Object userObject) {
this(userObject, true, null);
}// end IconNode
//* IconNode
/**Creates an IconNode Object*/
public IconNode(Object userObject, boolean allowsChildren
          , Icon icon) {
super(userObject, allowsChildren);
this.icon = icon;
}//end IconNode
//* setIcon
/**sets the Icon to the node*/
public void setIcon(Icon icon) {
this.icon = icon;
}// end setIcon
//* getIcon
/**gets the Icon of the node*/
public Icon getIcon() {
return icon;
}//end getIcon
//* getIconName
/**gets the IconName*/
public String getIconName() {
if (iconName != null) {
return iconName;
} else {
String str = userObject.toString();
int index = str.lastIndexOf(".");
if (index != -1) {
return str.substring(++index);
} else {
return null;
}//end getIconName
//* setIconName
/**sets the IconName*/
public void setIconName(String name) {
iconName = name;
}//end setIconName
}// end IconNodepublic class IconNode extends DefaultMutableTreeNode {
protected Icon icon;
protected String iconName;
//* IconNode
/**Creates an IconNode Object*/
public IconNode() {
this(null);
}//end IconNode
public class DNDTree extends JTree
public DNDTree (IconNode top)
super (top);
DNDTreeHandler dndHandler = new DNDTreeHandler(this);
setAutoscrolls(true);
public class DNDTreeHandler extends DnDHandler
TransferableDataItem transDataItem = new TransferableDataItem();
public DNDTreeHandler(Component dndComponent)
super(dndComponent);
public DataFlavor[] getSupportedDataFlavors()
return transDataItem.getTransferDataFlavors();
* Gets the data from the selected object being dragged
* @return node DataItem being dragged
public Transferable getTransferable()
TreePath path = getSelectionPath();
//DefaultMutableTreeNode selection = (DefaultMutableTreeNode)path.getLastPathComponent();
IconNode selection = (IconNode)path.getLastPathComponent();
if (path == null)
return(null);
else
TransferableDataItem node = new TransferableDataItem(selection);
return(node);
* Handles the drop of the component after the drag is complete
* @param transferable Object being dropped
* @param event drop event information
public void handleDrop(Transferable transferable, DropTargetDropEvent event) throws Exception
Point location = event.getLocation();
//Get path of node where object being dropped
TreePath path = getClosestPathForLocation(location.x, location.y);
//No drop target
if(path == null)
System.err.println ("Rejected");
event.rejectDrop();
else
Transferable tr = event.getTransferable();
if(tr.isDataFlavorSupported(TransferableDataItem.DEFAULT_MUTABLE_DATAITEM_FLAVOR))
System.out.println("Got transfered data");
Object userObject = tr.getTransferData(TransferableDataItem.DEFAULT_MUTABLE_DATAITEM_FLAVOR);
//DefaultMutableTreeNode node = (DefaultMutableTreeNode)path.getLastPathComponent();
IconNode node = (IconNode)path.getLastPathComponent();
IconNode newNode = new IconNode(userObject);
DefaultTreeModel model = (DefaultTreeModel)getModel();
model.insertNodeInto(newNode, node, 0);
else
System.err.println ("Rejected");
event.rejectDrop();
public abstract class DnDHandler implements DropTargetListener,DragSourceListener, DragGestureListener
protected DropTarget dropTarget;
protected DragSource dragSource;
public DnDHandler(Component dndComponent)
dropTarget = new DropTarget (dndComponent, this);
dragSource = new DragSource();
dragSource.createDefaultDragGestureRecognizer( dndComponent, DnDConstants.ACTION_COPY_OR_MOVE, this);
//creates transferable based on what's selected or returns null if nothings selected
protected abstract Transferable getTransferable();
protected abstract void handleDrop(Transferable transferable, DropTargetDropEvent event) throws Exception;
protected abstract DataFlavor[] getSupportedDataFlavors();
public void dropFailed (DragSourceDropEvent event)
System.out.println("Drop Failed");
public void dropSuccess (DragSourceDropEvent event)
System.out.println("Drop was successful");
private boolean isTransferableSupported(Transferable t)
DataFlavor[] flavors = getSupportedDataFlavors();
for (int i=0; i<flavors.length; i++)
if (t.isDataFlavorSupported(flavors) )
return true;
return false;
public void dragGestureRecognized( DragGestureEvent event)
Transferable trans = getTransferable();
Cursor dragIcon = getDragCursor(event);
if (trans != null)
// Starts the dragging
dragSource.startDrag (event, dragIcon, trans, this);
else
System.out.println( "nothing was selected");
* a drop has occurred
public void drop (DropTargetDropEvent event)
try
Transferable transferable = event.getTransferable();
// we accept only Strings
if (isTransferableSupported (transferable))
event.acceptDrop(event.getDropAction());
handleDrop(transferable, event);
event.getDropTargetContext().dropComplete(true);
else
event.rejectDrop();
catch (Exception e)
e.printStackTrace();
System.err.println( "Drop Exception" + e.getMessage());
event.rejectDrop();
//DragSourceListener interfaces
* is invoked when you are dragging over the DropSite
public void dragEnter (DropTargetDragEvent event)
// debug messages for diagnostics
//System.out.println( "dragEnter");
int action = event.getDropAction();
event.acceptDrag (action);
* is invoked when you are exit the DropSite without dropping
public void dragExit (DropTargetEvent event)
//System.out.println( "dragExit");
* is invoked when a drag operation is going on
public void dragOver (DropTargetDragEvent event)
//System.out.println( "dragOver");
* is invoked if the use modifies the current drop gesture
public void dropActionChanged ( DropTargetDragEvent event )
//gets the cursor to start drag
protected Cursor getDragCursor( DragGestureEvent event)
if (event.getDragAction() == DnDConstants.ACTION_MOVE)
return DragSource.DefaultMoveDrop;
if (event.getDragAction() == DnDConstants.ACTION_COPY_OR_MOVE)
return DragSource.DefaultCopyDrop;
else
return Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
* this message goes to DragSourceListener, informing it that the dragging
* has ended
public void dragDropEnd (DragSourceDropEvent event) {
if ( event.getDropSuccess())
dropSuccess(event);
else
dropFailed(event);
* this message goes to DragSourceListener, informing it that the dragging
* has entered the DropSite
public void dragEnter (DragSourceDragEvent event)
//System.out.println( " dragEnter");
* this message goes to DragSourceListener, informing it that the dragging
* has exited the DropSite
public void dragExit (DragSourceEvent event)
//System.out.println( "dragExit");
* this message goes to DragSourceListener, informing it that the dragging is currently
* ocurring over the DropSite
public void dragOver (DragSourceDragEvent event)
//System.out.println( "dragExit");
* is invoked when the user changes the dropAction
public void dropActionChanged ( DragSourceDragEvent event)
//System.out.println( "dropActionChanged");
public class TransferableDataItem extends DefaultMutableTreeNode implements Transferable
final static int DATA_ITEM = 0;
final static int STRING = 1;
final static int PLAIN_TEXT = 2;
//final public static DataFlavor DEFAULT_MUTABLE_DATAITEM_FLAVOR =
// new DataFlavor(DefaultMutableTreeNode.class, "Default Mutable Data Item");
final public static DataFlavor DEFAULT_MUTABLE_DATAITEM_FLAVOR =
new DataFlavor(IconNode.class, "Default Mutable Data Item");
static DataFlavor flavors[] = {DEFAULT_MUTABLE_DATAITEM_FLAVOR, DataFlavor.stringFlavor, DataFlavor.plainTextFlavor};
private Object data;
public TransferableDataItem()
public TransferableDataItem(Object data)
this.data = data;
public DataFlavor[] getTransferDataFlavors()
return flavors;
public Object getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException, IOException
Object returnObject;
if (flavor.equals(flavors[DATA_ITEM]))
returnObject = data;
else if (flavor.equals(flavors[STRING]))
returnObject = data.toString();
else if (flavor.equals(flavors[PLAIN_TEXT]))
returnObject = new ByteArrayInputStream(data.toString().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;
public class DNDList extends JList
DropTarget dropTarget;
public DNDList()
DNDListHandler dndHandler = new DNDListHandler(this);
setModel(new DefaultListModel());
//private void addElement(Point location, Object element)
private void addElement(Point location, IconNode element)
int index = locationToIndex(location);
// If index not found, add at end, otherwise add one beyond position
if (index == -1) {
index = getModel().getSize();
else
index++;
((DefaultListModel)getModel()).add(index, element);
public class DNDListHandler extends DnDHandler
TransferableDataItem transDataItem = new TransferableDataItem();
public DNDListHandler(Component dndComponent)
super(dndComponent);
public DataFlavor[] getSupportedDataFlavors()
return transDataItem.getTransferDataFlavors();
public DataFlavor[] getTransferDataFlavors()
return transDataItem.getTransferDataFlavors();
* Gets the data from the selected object being dragged
* @return node Node being dragged
public Transferable getTransferable()
Object selectedItem = getSelectedValue();
System.out.println("Selected Value is " + selectedItem);
TransferableDataItem item = new TransferableDataItem(selectedItem);
return(item);
* Handles the drop of the component after the drag is complete
* @param transferable Object being dropped
* @param event drop event information
public void handleDrop(Transferable transferable, DropTargetDropEvent event) throws Exception
Transferable tr = event.getTransferable();
Point location = event.getLocation();
if (tr.isDataFlavorSupported(TransferableDataItem.DEFAULT_MUTABLE_DATAITEM_FLAVOR))
//event.acceptDrop (DnDConstants.ACTION_COPY_OR_MOVE);
// Object userObject = tr.getTransferData(TransferableDataItem.DEFAULT_MUTABLE_DATAITEM_FLAVOR);
IconNode userObject = (IconNode)tr.getTransferData(TransferableDataItem.DEFAULT_MUTABLE_DATAITEM_FLAVOR);
addElement(location, userObject);
//dropTargetDropEvent.getDropTargetContext().dropComplete(true);
else
System.err.println ("Rejected");
event.rejectDrop();

I think your problem come from this method :
if (tr.isDataFlavorSupported(TransferableDataItem.DEFAULT_MUTABLE_DATAITEM_FLAVOR))
   System.out.println("Got transfered data");
   Object userObject = tr.getTransferData(TransferableDataItem.DEFAULT_MUTABLE_DATAITEM_FLAVOR);
//DefaultMutableTreeNode node = (DefaultMutableTreeNode)path.getLastPathComponent();
   IconNode node = (IconNode)path.getLastPathComponent();
   IconNode newNode = new IconNode(userObject);
   DefaultTreeModel model = (DefaultTreeModel)getModel();
   model.insertNodeInto(newNode, node, 0);
...You should write :
if (tr.isDataFlavorSupported(TransferableDataItem.DEFAULT_MUTABLE_DATAITEM_FLAVOR))
   IconNode node = (IconNode)tr.getTransferData(TransferableDataItem.DEFAULT_MUTABLE_DATAITEM_FLAVOR);
   IconNode node = (IconNode)path.getLastPathComponent();
   IconNode newNode = new IconNode(node);
   DefaultTreeModel model = (DefaultTreeModel)getModel();
   model.insertNodeInto(newNode, node, 0);
...and create a construcor for IconNode :
public IconNode(IconNode node) {
   super(node.getUserObject, node.getAllowsChildren());
   icon = node.icon;
}When you use IconNode newNode = new IconNode(userObject), the icon is null (see your constructor).
If I can say something, you should simplify your code : you can do the same thing dividing the size of your code by five (at less).
Denis

Similar Messages

  • Is there a way to change the icon associated with icloud account?

    Is there a way to change the icon associated with my iCloud account? It's currently a rose and i do not like it.

    Welcome to the Apple Support Communities
    Are you referring to your iCloud account picture or your user picture?
    It looks like you are referring to your user picture. To change it, open System Preferences > Users & Groups, press the picture box and choose the user picture you want from the ones that came by default. If you want to select a different picture stored on your Mac, drag that photo onto the picture box.
    If you want to change your iCloud account picture, open http://www.icloud.com and log in with your Apple ID. Then, press your name at the top right of the site and press the user picture. If you put the cursor on the picture box, you will see a button at the top of the box to change your picture

  • I've lost the icons associated with my bookmarks. I've tried restarting my Mac, reinstalling Safari, and the add-ons suggested in other forums without any success. Is there a way to fix this issue?

    I've lost the icons associated with my bookmarks. I've tried restarting my Mac, reinstalling Safari, and the add-ons suggested in other forums without any success. Is there a way to fix this issue?

    Hi rmmilleriii,
    You should take a look at [https://support.mozilla.org/kb/latest-firefox-issues#os=mac&browser=fx9 this article] that discusses some of the issues with the latest build of Firefox.
    There is some information in there that should help you to resolve your issue. Luckily, it is a very easy fix.
    Hopefully this helps!

  • My ipad unable to drag the slider, tried on restart, no reponse. How to solve this?

    My ipad is not hang, just unable to drag the slider to access it.
    I had tried many times on "Hold down the Sleep (On-Off) and Home buttons together for about 10-15 seconds or until the Apple Logo appears - ignore the red slider if it appears - then let go of the buttons. The device should begin to start up"
    It appeared apple logo, the device is restart, however still unable to drag the slider.
    It is faulty on the screen?

    Hey there AndreaYong,
    It sounds like your Touchscreen on the iPad isnt working correctly. I would recommend the troubleshooting info in the following article to help you figure out if this is a hardware issue or not:
    If the screen on your iPhone, iPad, or iPod touch doesn't respond to touch
    Make sure your hands are clean and dry, then try these steps:
    If you have a case or screen protector on your device, try removing it.
    Clean the screen with a soft, slightly damp, lint-free cloth.
    Unplug your device.
    Restart your device. If you can't restart, reset your device.
    If your touchscreen still doesn't respond like it should, contact Apple Support or take your device to an Apple Retail Store or Apple Authorized Service Provider.
    Thank you for using Apple Support Communities.
    Take care,
    Sterling

  • We were unable to find any subscriptions associated with your account

    Hi all,
    I am having a difficult time getting into my account. It was working just fine for a while but now I get "We were unable to find any subscriptions associated with your account" every time I go to manage.windowsazure.com

    Hi,
    I suggest you open the IE in new session, then try again.
    If still met the same issue, I think this issue might be related to account, this is out of support in this forum, I suggest you contact with azure support at:
     http://www.windowsazure.com/en-us/support/contact/
    Best Regards,
    Jambor
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click
    HERE to participate the survey.

  • Strange icon associated with Firefox

    Windows 7 Pro
    Firefox 35.0.1
    Yesterday I noticed that when I use ALT-TAB to switch between running programs on my desktop there was a new icon I'd never seen before. There was no text label on it, but if I went to it, I actually went to FF. I confiremed it is associated with FF by closing every other task. The icon only appears when FF is running. There is nothing under windows task manager to associate with it.
    It looks like this:

    I may have found the solution myself. Or at least a clue. While investigating something totally unrelated, I was working with ProcessExplorer and noticed in its process navigation pane, the same icon, associated with Trend Micro Office Scan.

  • How to keep icons associated with individual bookmarks?

    Hi There,
    When I changed from windows explorer the sites in WE (favorites) automatically showed up in Firefox bookmarks; only the icons associated with the individual bookmark (favorites) sites do not show up.
    I should note that they show up once the bookmark is opened but disappears again when Firefox is closed.
    It is much easier to identify a site listing with a visually icon then without one.
    Thanks

    Go to the App Store app and open that then tap on update bottom right.  Select all at the top left. The apps that you see there with the cloud logo have been downloaded with the ID your are logged in with but have been deleted from the phone, the ones with open written beside them are installed on your phone downloaded with your ID.  Any other apps on the screens on your phone would be the ones downloaded with the other ID

  • Recenty, some of the icons associated with websites will no longer display, although they always did so in the past

    The small icon that is associated with a website & appears to the left in the address bar when a website loads no longer appears for 6 of my most frequently used websites. This began about a week ago.

    Maybe this bug:
    *[https://bugzilla.mozilla.org/show_bug.cgi?id=701297 bug 701297] - Several favicons are lost in bookmarks with the upgrade to Firefox 8
    ''(please do not comment in bug reports)''

  • The image on the icon associated with my shortcut keeps disappearing. The shortcut still works.

    The Firefox image associated with the shortcut I have pinned to the taskbar has disappeared. The shortcut still works but the image is gone. I have uninstalled and reinstalled 5.0. This worked, but after a few days the image disappeared again.

    Nope. under security you can set an administrator password, and under accounts, you can set up a guest account and limit the things that person can access. as for computer access, again, it depends on the privileges you choose as creator of the guest account.
    jB

  • I am Unable to remember Icould ID associated with my iphone?

    I have an iphone 4 and I didn't know anything about the Activation Lock.  I sold my phone on Ebay and I can't unlock the phone because I absolutely can't remember the icloud info associated with the phone?  I reset the phone before I shipped it.  The buyer returned the phone and I am trying to figure out what can I do to unlock the phone.

    Start here:
    https://iforgot.apple.com

  • Mac Platform - I am unable to drag program icon to dock - must now use an alias on my MacBook

    Since upgrading Firefox this most recent time, I am unable to pull a logo placeholder into the dock on my MacBook. I have to create an alias of Firefox and use that in the dock. Why this change? Is there something else I can do?
    Also where is the option in the Firefox menu that I can use to upgrade the program over again?
    Thank you,
    Kathleen Graves

    For the past 2 days, I have been unable to open the aperature application.
    What happened directly before this started? Have you updated software, installed something new, imported new images? Run any applications to clean the mac?
    And what is your Aperture version and MacOS X version? Is your profile signature still current (MacBook Pro, Mac OS X (10.7.2), i just upgraded to lion)?
    Have you checked, if Aperture is still installed in your Applications folder? Does it start, if you click it directly in Applications and not from the Dock?  Does Aperture start, if you click it while holding down the ⌥-key and select a different aperture library not on your 2 TB drive?
    Regards
    Léonie

  • F5 no longer refreshes pages, neither does the icon associated with refreshing. I'm not asking a question, it's just a bug from the lastest update.

    I tried looking at an older version but it is uncompact and a little difficult to understand where to put the files.
    Also not a fan of refresh icon being inside the address bar.

    Can you try to start Firefox in Safe Mode to see if Firefox works properly with no error? You can start Firefox in Safe Mode below:
    *'''Windows/Mac''': Go to Help > Restart with add-ons disabled
    *'''Linux''': Run ''firefox -safe-mode'' in the Terminal/Konsole
    If Firefox opens up fine with no problems, it's probably one of your extensions that's causing the issue. You can re-enable your add-ons one by one until you find the one that causes the issue upon being re-enabled.

  • Can anyone tell me what the Speedometer-like icon associated with my USB drive partitions mean?

    This seems to be new to Yosemite.  Or at least I have never seen it until recently.
    In Disk Utility (and Carbon Copy Cloner - which I use to create a bootable copy of Macintosh HD to a USB HD partition) there is an icon that looks like a speedometer or perhaps a thermometer).  The needle is all the way to the right in the red portion of the icon, which looks rather ominous.
    It is easier to see in Carbon Copy Cloner than it is in Disk Utility, but exists in both applications.
    Can anyone tell me if this icon is coming from Yosemite (or perhaps Carbon Copy Cloner) and exactly what it means? 
    The drive passes S.M.A.R.T and general Disk Utility testing and appears to behave normally.  I do not believe the drive (a five year old 1 TB WD My Book Essentials 2.0) supports temperature monitoring so I doubt it is a thermometer.  And the icon does NOT appear next to the partition used for Time Machine backups but does appear next to the other two GUID, Mac OS Extended (Journaled) partitions.
    Thanks.

    ricfromkennewick,
    It comes from installing an enhancing utility made by Western Digital (WD), Its supposed to and I quote "improve the performance of the drive on a Mac Computer." It comes in a folder, possibly call extra, already on the drive when you get it. If you go to http://support.wdc.com/product/download.asp?level1=1&lang=en and find your drive you can pull up the specific user guide to learn more.
    Hope that helps,
    westongallagher

  • Unable to drag and drop images with manual sort in CS2 bridge

    I've had CS2 for several years now but for some reason, just never really needed to do a manual sort. Now that I DO need to, it doesn't seem to be working. I have the view set for manual and no matter what kind of working space I use, the program does not allow me to drag and drop the thumbnails into different positions. All I get is that circle with a diagonal line through it, showing me that what I'm trying isn't going to work.
    Any ideas?
    Windows XP
    2G of ram
    Bridge version 1.0.4.6 (104504)
    CS2

    DUUUUUUUDE! IT'S WORKING NOW!!!!
    When you originally asked, "What happens if you click on an image and move it to another folder?", I didn't really understand what you were talking about because I almost never have my workspace set up in a way that the folders are showing. I always hide them. So I didn't really understand what you were saying. Just a few minutes ago, I realized that that is what you were asking so I opened up the folder area and tried dragging thumbs into different folders and saw that it does work.
    So then, because of you mentioning that blue line and me know that I did see the blue line on occasion. I went back over into the thumbnails to find out if I always got the blue line or if it was a hit or miss thing. I couldn't believe my eyes when all of the sudden I saw that I no longer was getting that circle with the line through it, and low and behold, the thumbnail dropped right into position like it is supposed to!!!!
    The thing is, I don't know what I've learned from this. I thought, "well, what is different?" The only thing that I can think of is that I now have the folders showing in my workspace so I wondered if that was the secret but I just now hid the folders again like I usually do and the drag and drop into position is STILL WORKING!!!
    All I can figure is that somehow, dragging and dropping a thumbnail into a folder like that for the first time, triggered something so that I can now drag and drop among the thumbnail positions? Not sure. All I know is that for some reason, IT'S WORKING NOW!
    Now, for the second important part, I wonder if I drag and drop everything into the order that I want and then select all of the files and drag them into a new folder, will they stay in that order? I need to go experiment with that.
    If you hadn't kept at it and in turn had me keep trying stuff, it would have never happened because I had pretty much given up.
    Thanks Curt!
    You da man!!!

  • I do not have an add to contacts icon associated with text messages how do I add them?

    I want to add people I message to my contact list but the only options I have are compose and edit/delete. What do I need to do?

    click the arrow on the right in the message-list and scoll to the top. There you have 3 buttons: call / FaceTime / Contact

Maybe you are looking for

  • How to dispaly items of Particular Sorder of IT into single row in alv dis?

    Hi, Experts, I have an Sales order internal table along with its corresponding items i want to display those items in side by side in alv display. Ex: Vbeln   posnr   netwr    waerk 56800   10       21.00    Col 56800   20       56800   30       5680

  • Trouble with form on a view

    Hi, I have trouble with the ROWID (ORA-01445) error when running the standard doQuery function on a button in a Form. Is it possible to use doQuery or do I have to write my own PL/SQL code, if so where can I find information on this (like the code fo

  • Watching DVDs & other video via PBG412" & HDTV

    Hello. I've been using a PBG412" as a media center for the last 4+ years. I have a remote attached (third party infra-red remote) and it works great! This winter my wife and I are considering a new TV for the first time in, well, a long time. Right n

  • Prime Infrastructure 2.1 Compliance Licensing

    Till Date LMS and Cisco Prime Infrastructure are standalone applications and do not operate in sync. If you deploy a new application server for LMS, it will be working separately and you can have its compliance manager running with this license appli

  • Can't copy more than 8.4MB through vpn

    I have a vpn set up on Lion server (imac).  I connect remotely from a snow leopard mbp client.  Connecting is fine, mounting folders is fine, and copying small files is fine.  When I try to copy a file (quicktime movie 8.8MB and another 57MB)  from t