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());
}
}
}
}