Reputation: 13
I am new in java. I want to make a drag and drop application by java swing. Where, there will be some JLabel source(label1,label2,label3) and destination(labelD1,labelD2,labelD3) for drag and drop. A label can be dragged and dropped only once, there should be a warning message if a label is dragged second time which is already dragged and dropped to any of the destination label. I tried to use addMouseMotionListener but could not solve it. It would be a great help if I get any suggestion what should be the proper way.
My code is given bellow:
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseMotionAdapter;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.TransferHandler;
public class Editor extends JFrame{
String selectedLable = "";
JLabel label1,label2, label3, labelD1, labelD2, labelD3;
public Editor() {
super("Drag and Drop Image");
label1 = new JLabel("label1");
label2 = new JLabel("label2");
label3 = new JLabel("label3");
labelD1 = new JLabel();
labelD2 = new JLabel();
labelD3 = new JLabel();
label1.setBounds(20, 30, 50, 50);
label2.setBounds(100, 30, 50, 50);
label3.setBounds(180, 30, 50, 50);
labelD1.setBounds(20, 200, 50, 20);
labelD2.setBounds(100, 200, 50, 20);
labelD3.setBounds(180, 200, 50, 20);
labelD1.setBorder(BorderFactory.createLineBorder(Color.black));
labelD2.setBorder(BorderFactory.createLineBorder(Color.black));
labelD3.setBorder(BorderFactory.createLineBorder(Color.black));
MouseListener ml = new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
JComponent jc = (JComponent)e.getSource();
TransferHandler th = jc.getTransferHandler();
th.exportAsDrag(jc, e, TransferHandler.COPY_OR_MOVE);
//can i get the source label and check, is label already in
//global variable,show warning if its dragged and dropped
//once??
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseClicked(MouseEvent e) {
}
};
/*
label1.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
// can i set the sourse label to a global variable and
// and check it mousePressed??
}
});
*/
label1.addMouseListener(ml);
label2.addMouseListener(ml);
label3.addMouseListener(ml);
label1.setTransferHandler(new TransferHandler("text"));
label2.setTransferHandler(new TransferHandler("text"));
label3.setTransferHandler(new TransferHandler("text"));
labelD1.setTransferHandler(new TransferHandler("text"));
labelD2.setTransferHandler(new TransferHandler("text"));
labelD3.setTransferHandler(new TransferHandler("text"));
add(label1);
add(label2);
add(label3);
add(labelD1);
add(labelD2);
add(labelD3);
setLayout(null);
setSize(500,500);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new Editor();
}
}
Upvotes: 0
Views: 96
Reputation: 7
Something that comes to mind is that you should implement your own JLabel class (by extending the original) and adding a boolean member, to denote whether or not it was previously dragged. Then in your listener method, before actually moving the label check if the label's boolean member is true or false, and display your warning message accordingly.
Upvotes: 0
Reputation: 324098
I tried to use addMouseMotionListener
Maybe you can use a ComponentListener
? You could listen for a componentMoved
event. Then you can remove the DnD functionality from the label.
I would guess you could do something like:
JLabel label = (JLabel)event.getSource();
label.setTransferHandler( null );
Upvotes: 0