2

I've implemented this very basic, drag and drop between two JPanels, but this doesn't really meet my requirements!

public class test extends JFrame {

{    JPanel mainpanel, storypanel, imageselect;



    public test(){

          mainpanel = new JPanel(new BorderLayout());
          storypanel = new JPanel();
          imageselect = new JPanel();
            MouseListener listener = new MouseAdapter(){
            public void mousePressed(MouseEvent e)
        {
            JComponent c = (JComponent) e.getSource();
            TransferHandler handler = c.getTransferHandler();
            handler.exportAsDrag(c, e, TransferHandler.COPY);
        }
        };

            int j = 0;
            BufferedImage myImages;
            JLabel imgselect = new JLabel();
            try { myImages = ImageIO.read(new File("four.jpg"));
            //myImages[j] = resize(myImages[j]);
            imgselect= new JLabel(new ImageIcon(myImages));
            System.out.println(j);
            imageselect.add(imgselect);
            imgselect.addMouseListener(listener);
            imgselect.setTransferHandler(new TransferHandler("icon"));

            } catch(Exception e) {};

            int i = 0;
            BufferedImage storyimages;
            JLabel storylabel = new JLabel();


            //targetImg = new ImageIcon("lmkpackage/base/TargetImg.jpg");
            try{ storyimages = ImageIO.read(new File("TargetImg.jpg"));
            //storyimages[i] = resize(storyimages[i]);
            storylabel = new JLabel(new ImageIcon(storyimages));
            System.out.println(i);
            storypanel.add(storylabel); 
            storylabel.addMouseListener(listener);
            storylabel.setTransferHandler(new TransferHandler("icon"));
            } catch(Exception e) {};


            mainpanel.add(imageselect, BorderLayout.NORTH);
            mainpanel.add(storypanel, BorderLayout.SOUTH);

            getContentPane().add(mainpanel);



        }

        public static void main(String[] args){

        System.out.println("Application Running");
        JFrame mainframe =  new test();
            mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainframe.setSize(1000,1000);
            mainframe.setVisible(true);

        }

    }

Sorry I wasn't very clear here. Im trying to drag an ImageIcon from a JLabel on the imageselect panel, to another JLabel the storyline panel. I can do this, with the above code. But when I do this, I can drag an ImageIcon from the imageselect panel, and replace another ImageIcon on the same panel. I do NOT want this to happen. I can also drag from the storyline panel to the imageselect panel, which I do NOT want.

I'm not asking to be spoon fed code, I'm just looking for a push in the right direction!

EDIT: I'm wondering is there any way of counting a successful drag and drop operation, the solution below answered my original question.

Peddler
  • 6,045
  • 4
  • 18
  • 22
  • please which one of tutorials, DnD is maybe night_mare in the Swing, but JLabel with Image isn't somehow compliced and works – mKorbel Feb 28 '12 at 17:21
  • 3
    Reject the drop on the 'from' panel? For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Feb 28 '12 at 20:02
  • @AndrewThompson I have updated my question with more detail – Peddler Feb 28 '12 at 22:25
  • @Peddler a snippet out of context is still not an [SSCCE](http://sscce.org/). –  Feb 28 '12 at 22:27
  • @JarrodRoberson I didn't add that snippet in my last edit. – Peddler Feb 28 '12 at 22:29
  • @Peddler I didn't say you did, I was just pointing out it was still useless after the edit. –  Feb 28 '12 at 22:31
  • @JarrodRoberson I've updated again, let me know if this is still useless and I'll see what I can do... – Peddler Feb 28 '12 at 23:02
  • @AndrewThompson good idea - made me flesh it out a bit, now that there's an SSCCE :-) As an aside, nice question - the first example of how property transferHandlers can be useful :-) – kleopatra Feb 29 '12 at 11:36

1 Answers1

5

This is basically an outline of @Andrew's comment - easily possible due to your SSCCE :-)

Subclass TransferHandler, override canImport to check if the source label is on the imageSelect panel and reject if so.

    // custom transferHandler which decides about imports based on source
    TransferHandler handler = new TransferHandler("icon") {

        @Override
        public boolean canImport(TransferSupport support) {
            return super.canImport(support) 
                    && support.getComponent().getParent() != imageSelectPanel;
        }

    };
    // use the handler on all labels (handlers can be shared, btw)
    // for each label on imageSelectPanel
    imageSelectLabel.setTransferHandler(handler)
    // for each label on the target panel (aka storyPanel) 
    storyLabel.setTransferHandler(handler)
kleopatra
  • 51,061
  • 28
  • 99
  • 211