Results 1 to 3 of 3

Thread: DnD problem

  1. #1
    mix99 is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

    Default DnD problem

    Ho guys i want just your idea about the following problem:
    i have a jlabel than implements a droptarget listener...it makes some operation when i drop an image inside(this image is dragged from another component)...
    I would to limitate the drop area inside the component without(or with less) modify the droptarget method. I create a polygon inside jlabel and i would that droplistener calls drop methods only when mouse goes inside polygon area...any idea? Thx you in advance guys...

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    I made up a test app to find out.
    Here's some of the drop target listener code.
    Java Code:
    class DropLabel extends JLabel {
        DropTarget dropTarget;
        DropTargetListener targetListener;
        int actions = DnDConstants.ACTION_COPY;
        Polygon polygon;
    
        public DropLabel(ImageIcon icon) {
            super(icon);
            targetListener = new TargetListener();
            // component, actions, DropTargetListener, accepting
            dropTarget = new DropTarget(this, actions, targetListener, true);
        }
    
        private class TargetListener implements DropTargetListener {
            ...
            public void dragOver(DropTargetDragEvent dtde) {
                Point loc = dtde.getLocation();
                // Drop point must be over polygon.
                if(!isDragOK(dtde) || !polygon.contains(loc)) {
                    dtde.rejectDrag();
                    return;
                }
                dtde.acceptDrag(dtde.getDropAction());
            }
    
            public void drop(DropTargetDropEvent dtde) {
                // Check for congruity of actions.
                ...
    
                // Recover image from transferable data.
                BufferedImage image = ...
    
                // Check drop location is over polygon.
                Point loc = dtde.getLocation();
                if(!polygon.contains(loc)) {
                    dtde.dropComplete(false);
                    return;
                }
                dtde.dropComplete(true);
                setIcon(new ImageIcon(image));
            }
            ...
        }
        ...
    }

  3. #3
    mix99 is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

Similar Threads

  1. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 06:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •