1

I am new in Swing. I have requirement to implement drag and drop using swing. In current frame contains different panels i.e.,Center Panel & LeftPanel have sub panels i.e., Controls Panel,Properties Panel.I trying to drag and drop the labels/jbuttons/Images from Control Panel into Center Panel.These panels are used BorderLayout

enter image description here

I tried drag and drop with Mouse Event.I put mouse listner on Control Panel. When I dropping the label/image into center panel.It reads -X & Y Cooridnates and check the code

private void mak_lis(final SLabel l) {
   l.addMouseListener(new MouseAdapter() {
    public void mousePressed(MouseEvent m) {
       System.out.println("mak_lis Mouse mousePressed");

    setCursor(yd);
    // l.setBorder(new MatteBorder(1,1,1,1,Color.black));

    }
    public void mouseReleased(MouseEvent m) {
       l.setBorder(null);
       setCursor(dc);
       System.out.println("mak_lis Mouse mouseReleased");
       int x = -(m.getX() + l.getX() - leftPanel.getX());
       int y = m.getY() + l.getY() + leftPanel.getY();
       System.out.println("mak_lis Mouse mouseReleased" + "x" + x+ "y" + y);
       if (y > 0 && x > 0 && y < leftPanel.getHeight() && x < leftPanel.getWidth()) {
        leftPanel.add(new_lab(l, x, y));
                leftPanel.repaint();
        Component[] components1 = leftPanel.getComponents();
        Component component = null;
        for (int i = 0; i < components1.length; i++) {
          // System.out.println("components iii"+ components1[i]);
        component = components1[i];

            }

    }
}

});
}

Please tell me is there any missing here. What is best approches to achieve drag and drop functionality?

Haldean Brown
  • 12,411
  • 5
  • 43
  • 58
tech2504
  • 947
  • 4
  • 19
  • 34
  • *"I am implementing designer tool using swing."* A D'n'D GUI designer? (shudder) There are already too many of them creating problems that GUI programmers have to fix later. – Andrew Thompson Jan 10 '13 at 05:40
  • 1
    @AndrewThompson I would say there are too many programmers creating problems with tools that they don't understand ;) – MadProgrammer Jan 10 '13 at 06:15
  • possible duplicate of [How can I set the priority mouse listener](http://stackoverflow.com/questions/14273923/how-can-i-set-the-priority-mouse-listener) – David Kroukamp Jan 11 '13 at 13:40

1 Answers1

4

This is not how drag and drop is accomplished in Java/Swing

There are plenty of examples on SO...

I'd also recommend that you checkout How to drag and drop with Java 2

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • As my requirment,I dont any JCompenent in dropped panel.So how Can i dropped JCompnent into this panel.Please suggest how to approach these scenerio? – tech2504 Jan 10 '13 at 19:33
  • You can't. You MUST have a active component, attached to a native peer, displayed on the screen in order to have the ability to receive mouse events, that's how Swing/Java works. How do you expect to be able to "paint" something on the screen if it's not some kind of component? – MadProgrammer Jan 10 '13 at 19:43
  • 1
    +1 also see my [approach](http://stackoverflow.com/a/14276466/1133011) which shows how to drag and drop swing components like JComponents between JPanels etc – David Kroukamp Jan 11 '13 at 13:39