Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8959091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:22:09+00:00 2026-06-15T15:22:09+00:00

I have defined a custom canvas style component, using JPanel, that will support the

  • 0

I have defined a custom canvas style component, using JPanel, that will support the dragging of objects onto the canvas. What I can’t seem to figure out is how to change the drag and drop (DnD) cursor to a custom one, using a TransferHandler. For example, instead of the default link cursor during DnD, I want substitute my own. Is there a way to do this using a TransferHandler?

I suspect I will have to use the AWT DnD support to do this but I am hoping to avoid that if I can.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:22:11+00:00Added an answer on June 15, 2026 at 3:22 pm

    By digging into the TransferHandler code I found a work around. The dragOver method is where I change the cursor. I still think I may be missing something simple but this will work for now.

    The two static class, as well as the code in the exportAsDrag are minorly modified copies of code from the TransferHandler source.

    EDIT – This is how I worked it out. Hope this helps. Suggestions welcome.

        public class OverrideIconTransferHandler extends TransferHandler {
        private class MyDragGestureRecognizer extends DragGestureRecognizer {
    
            private static final long serialVersionUID = 1L;
    
            MyDragGestureRecognizer(DragGestureListener dgl) {
                super(DragSource.getDefaultDragSource(), null, NONE, dgl);
            }
    
            void gestured(JComponent c, MouseEvent e, int srcActions, int action) {
                setComponent(c);
                setSourceActions(srcActions);
                appendEvent(e);
                fireDragGestureRecognized(action, e.getPoint());
            }
    
            @Override
            protected void registerListeners() {
            }
    
            @Override
            protected void unregisterListeners() {
            }
    
        }
    
        private class MyDragHandler implements DragGestureListener, DragSourceListener {
    
            private boolean scrolls;
    
            @Override
            public void dragDropEnd(DragSourceDropEvent dsde) {
                DragSourceContext dsc = dsde.getDragSourceContext();
                JComponent c = (JComponent) dsc.getComponent();
                if (c.getTransferHandler() instanceof OverrideIconTransferHandler) {
                    OverrideIconTransferHandler t = (OverrideIconTransferHandler) c.getTransferHandler();
                    if (dsde.getDropSuccess()) {
                        t.exportDone(c, dsc.getTransferable(), dsde.getDropAction());
                    } else {
                        t.exportDone(c, dsc.getTransferable(), NONE);
                    }
                }
                c.setAutoscrolls(scrolls);
            }
    
            @Override
            public void dragEnter(DragSourceDragEvent dsde) {
            }
    
            @Override
            public void dragExit(DragSourceEvent dsde) {
            }
    
            @Override
            public void dragGestureRecognized(DragGestureEvent dge) {
                JComponent c = (JComponent) dge.getComponent();
                if (c.getTransferHandler() instanceof OverrideIconTransferHandler) {
                    OverrideIconTransferHandler th = (OverrideIconTransferHandler) c.getTransferHandler();
                    Transferable t = th.createTransferable(c);
                    if (t != null) {
                        scrolls = c.getAutoscrolls();
                        c.setAutoscrolls(false);
                        try {
                            Image im = th.getDragImage();
                            if (im == null) {
                                dge.startDrag(null, t, this);
                            } else {
                                dge.startDrag(null, im, th.getDragImageOffset(), t, this);
                            }
                            return;
                        } catch (RuntimeException re) {
                            c.setAutoscrolls(scrolls);
                        }
                    }
    
                    th.exportDone(c, t, NONE);
                }
            }
    
            @Override
            public void dragOver(DragSourceDragEvent dsde) {
                if (dropCursorOverrides.containsKey(dsde.getDropAction())) {
                    dsde.getDragSourceContext().setCursor(dropCursorOverrides.get(dsde.getDropAction()));
                } else {
                    dsde.getDragSourceContext().setCursor(null);
                }
            }
    
            @Override
            public void dropActionChanged(DragSourceDragEvent dsde) {
            }
        }
    
        private static final long serialVersionUID = 1L;
        private MyDragGestureRecognizer myRecognizer = null;
        private final Map<Integer, Cursor> dropCursorOverrides = new HashMap<>();
    
        public void addDropCursorOverride(final int action, final Cursor cursor) throws IllegalArgumentException {
            if (!(action == COPY || action == MOVE || action == LINK || action == NONE)) {
                throw new IllegalArgumentException("Unknown Action Type");
            }
            dropCursorOverrides.put(action, cursor);
        }
    
        @Override
        public void exportAsDrag(JComponent comp, InputEvent e, int action) {
            if (comp.getTransferHandler() instanceof OverrideIconTransferHandler) {
                int srcActions = getSourceActions(comp);
    
                if (!(e instanceof MouseEvent) || !(action == COPY || action == MOVE || action == LINK) || (srcActions & action) == 0) {
    
                    action = NONE;
                }
    
                if (action != NONE && !GraphicsEnvironment.isHeadless()) {
                    if (myRecognizer == null) {
                        myRecognizer = new MyDragGestureRecognizer(new MyDragHandler());
                    }
                    myRecognizer.gestured(comp, (MouseEvent) e, srcActions, action);
                } else {
                    exportDone(comp, null, NONE);
                }
            } else {
                super.exportAsDrag(comp, e, action);
            }
        }
    
        public void removeDropCursorOverride(final int action) throws IllegalArgumentException {
            if (!(action == COPY || action == MOVE || action == LINK || action == NONE)) {
                throw new IllegalArgumentException("Unknown Action Type");
            }
            dropCursorOverrides.remove(action);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have defined a custom IPrincipal and custom IIdentity based on a website that
Is it possible to have scoped macros using custom defined macros through boost wave?
I have a WPF application where I have defined custom commands that use the
I have a problem using my url view helper. I have defined custom routes
I have defined a custom target in cmake. I now want to ensure that
I have defined a custom Capistrano task that's supposed to run locally (on my
I have the following simple custom control that I have defined for a Windows
I am using magic fields and have defined a custom post type called collection
I have defined a UITabBar programmatically but how can I set a custom UITabBar
I have defined a custom user type that works fine when used properties of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.