Hi!

I'm trying to create a method for simultaneous dragging a group of JLabel components. First, I select the components to be dragged ( getSelectedStatus() -> true ), and then I want to be able to drag the selected components. I have a working method for a single JLabel component, but there is a problem when dragging a group. Could please someone help? My code snippet and some comments are given below.

class PanelMotion extends MouseAdapter
    {
        JLabel selectedLabel;
        Point offset;
        Rectangle r;
        boolean dragging;

        public PanelMotion()
        {
            offset = new Point();
            dragging = true;
        }
...
        @Override
        public void mouseDragged(MouseEvent e)
        {
            try {
                if((dragging) && (!Menu.getSelect())) // Menu.getSelect() returns 'false' if a group selection regime is turned off.
                {
                    r.x = e.getX() - offset.x;
                    r.y = e.getY() - offset.y;
                    selectedLabel.setBounds(r.x, r.y, r.width, r.height);
                    repaint();
                } else if ((dragging) && (Menu.getSelect())) {
// THE BELOW PIECE OF CODE DOESN'T WORK CORRECTLY
                    Point p = e.getPoint();
                    for (Component c : getComponents()) {
                        if((c instanceof MyLabel) && (((MyLabel)c).getSelectedStatus())) {
                            r = c.getBounds();
                            offset.x = p.x - r.x;
                            offset.y = p.y - r.y;
                            r.x = e.getX() - offset.x;
                            r.y = e.getY() - offset.y;
                            c.setBounds(r.x, r.y, r.width, r.height);
                        }
                    }
                    repaint();
                }
            } catch (Exception ex) {
                //System.out.println(ex.getMessage());
            }
        }
    }
}

I've tried to solve the problem by adding ArrayList, but it failed (see the error message below).

class PanelMotion extends MouseAdapter
    {
        JLabel selectedLabel;
        ArrayList offset;
        ArrayList r;
        boolean dragging;

        public PanelMotion()
        {
            offset = new ArrayList();
            r = new ArrayList();
            dragging = true;
        }

        @Override
        public void mouseClicked(MouseEvent e) {        
                Component[] c = getComponents();
                for(int i = 0; i < c.length; i++) {
                    if(c[i] instanceof JLabel) {
                      r.add(i, new Rectangle());
                      r.add(i,c[i].getBounds());
                      Point p = e.getPoint();
                      if(((Rectangle)(r.get(i))).contains(p)) {
                        remove(i);
                        repaint();
                      }
                    }
          }
        }

        @Override
        public void mousePressed(MouseEvent e)
        {
            Component[] c = getComponents();
            for(int i = 0; i < c.length; i++)
            {
                if((c[i] instanceof JLabel) && (((MyLabel)c[i]).getSelectedStatus()))
                {
                    r.add(i,c[i].getBounds());
                    Point p = e.getPoint();
                    if(((Rectangle)(r.get(i))).contains(p))
                    {
                        selectedLabel = (JLabel)c[i];
                        offset.add(i, new Point());
                        ((Point)(offset.get(i))).x = p.x - ((Rectangle)(r.get(i))).x;
                        ((Point)(offset.get(i))).y = p.y - ((Rectangle)(r.get(i))).y;
                        dragging = true;
                        break;
                    }
                }
            }
        }

        @Override
        public void mouseReleased(MouseEvent e)
        {
            dragging = false;
        }

        @Override
        public void mouseDragged(MouseEvent e)
        {
            try {
                   Point p = e.getPoint();
                   Component[] c = getComponents();
                   for(int i = 0; i < c.length; i++) {
                        if((c[i] instanceof JLabel) && (((MyLabel)c[i]).getSelectedStatus()))
                            ((Rectangle)(r.get(i))).x = e.getX() - ((Point)(offset.get(i))).x;
                            ((Rectangle)(r.get(i))).y = e.getY() - ((Point)(offset.get(i))).y;
                            c[i].setBounds(((Rectangle)(r.get(i))).x, ((Rectangle)(r.get(i))).y,
                                    ((Rectangle)(r.get(i))).width, ((Rectangle)(r.get(i))).height);
                        }
                    repaint();
              } catch (Exception ex) {
                //System.out.println(ex.getMessage());
            }
        }
    }
Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
        at java.util.ArrayList.add(ArrayList.java:367)
        at mainClassesPack.SelectablePanel$PanelMotion.mousePressed(SelectablePanel.java:261)
        at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:262)
        at java.awt.Component.processMouseEvent(Component.java:6264)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
        at java.awt.Component.processEvent(Component.java:6032)
        at java.awt.Container.processEvent(Container.java:2041)
        at java.awt.Component.dispatchEventImpl(Component.java:4630)
        at java.awt.Container.dispatchEventImpl(Container.java:2099)
        at java.awt.Component.dispatchEvent(Component.java:4460)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4235)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
        at java.awt.Container.dispatchEventImpl(Container.java:2085)
        at java.awt.Window.dispatchEventImpl(Window.java:2478)
        at java.awt.Component.dispatchEvent(Component.java:4460)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

I've almost solved the problem - there is no error message. However, the group of images cannot be dragged. This could happen, because if(((Rectangle)(r.get(i-1))).contains(p)) returns 'false'. Why actually it returns 'false'? Perhaps, someone could help? Thanks.

class PanelMotion extends MouseAdapter
    {
        JLabel selectedLabel;
        ArrayList offset;
        ArrayList r;
        boolean dragging;

        public PanelMotion()
        {
            offset = new ArrayList();
            r = new ArrayList();
            dragging = true;
        }

        @Override
        public void mousePressed(MouseEvent e)
        {
            Component[] c = getComponents();
            for(int i = 0; i < c.length; i++)
            {
                if((c[i] instanceof JLabel) && (((MyLabel)c[i]).getSelectedStatus()))
                {
                    r.add(new Rectangle());
                    ((Rectangle)(r.get(i-1))).x = c[i].getBounds().x;
                    ((Rectangle)(r.get(i-1))).y = c[i].getBounds().y;
                    Point p = e.getPoint();
                    if(((Rectangle)(r.get(i-1))).contains(p))
                    {                        
                        selectedLabel = (JLabel)c[i];
                        offset.add(new Point());
                        ((Point)(offset.get(i-1))).x = p.x - ((Rectangle)(r.get(i-1))).x;
                        ((Point)(offset.get(i-1))).y = p.y - ((Rectangle)(r.get(i-1))).y;
                        dragging = true;
                        break;
                    }
                }
            }
        }

        @Override
        public void mouseReleased(MouseEvent e)
        {
            dragging = false;
        }

        @Override
        public void mouseDragged(MouseEvent e)
        {
            try {
                   Point p = e.getPoint();
                   Component[] c = getComponents();
                   for(int i = 0; i < c.length; i++) {
                        if((c[i] instanceof JLabel) && (((MyLabel)c[i]).getSelectedStatus()))
                            ((Rectangle)(r.get(i-1))).x = e.getX() - ((Point)(offset.get(i-1))).x;
                            ((Rectangle)(r.get(i-1))).y = e.getY() - ((Point)(offset.get(i-1))).y;
                            c[i].setBounds(((Rectangle)(r.get(i-1))).x, ((Rectangle)(r.get(i-1))).y,
                                    ((Rectangle)(r.get(i-1))).width, ((Rectangle)(r.get(i-1))).height);
                        }
                    repaint();
              } catch (Exception ex) {
                //System.out.println(ex.getMessage());
            }
        }
    }

Now I can drag the images. I guess that I am very close to the final solution. What is still unsolved: when I start dragging the group of images, then ALL images take the same location...Help?

r.add(new Rectangle(c[i].getBounds().x, c[i].getBounds().y, c[i].getBounds().width, c[i].getBounds().height));
...
offset.add(new Point(p.x-c[i].getBounds().x, p.y-c[i].getBounds().y));

Uff, solved!!!

How you solved the problem ? Can you share code ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.