Reputation: 977
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
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?
Upvotes: 2
Views: 8780
Reputation: 347204
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
Upvotes: 4