Lab 2 – Drag and Drop
Objectives
- Understand the Drag and Drop
mechanism for Swing Components
- Write a TransferHandler
for Drag and Drop
- Open your previous lab
project in eclipse.
- You are going to extend this
GUI by adding drag and drop mechanism to the labels that hold the selected
icons. We want the ability to copy
the icon from one label to another by dragging. The tutorial for drag and drop is
available at http://java.sun.com/docs/books/tutorial/uiswing/misc/dnd.html
Each component that is either the source or the
destination of a drag and drop much have a TransferHandler.
- You will add the
default TransferHandler to each of your jLabels such as:
jLabel.setTransferHandler(new
myTransferHandler("icon"));
When you create a TransferHandler you specify
the java bean property that you want to transfer such as text, icon,
background, etc.
- Next you will add a mouseListener to each of the labels so that when the
mouse is pressed within the label it will initiate a drag and drop
operation by invoking the exportAsDrag
operation from the components TransferHandler.
jLabel.addMouseListener(new java.awt.event.MouseAdapter()
{
public void mousePressed(java.awt.event.MouseEvent e) {
JComponent c = (JComponent)e.getSource();
TransferHandler handler = c.getTransferHandler();
handler.exportAsDrag(c,
e, TransferHandler.COPY);
}
});
- Run your GUI and try
performing drag and drop operations on the labels. You should see that the icon from the
source of the drag gets copied to the dropped location.
- Add another jLabel with the trash can icon provided somewhere with
in your GUI. We are going to make
it so that when we drag an icon here it will get removed from our
array. Here the default TransferHandler just doesn’t do what we want. Instead, we are going to define two TransferHandler classes that extend the default. First take a look at the documentation
for the TransferHandler class: http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/TransferHandler.html
The important methods for our purposes here are exportAsDrag, exportDone,
and importData.
- Write an inner class to your GUI
called TrashTransferHandler that extends the TransferHandler class.
Override the importData method so that it
calls the super class importData and then
changes the trash icon to a full trash can. Test this out and see if it does what
you expect.
- Now write an inner class
called LabelTransferHandler that extends the TransferHandler class.
Override the exportAsDrag and exportDone methods so that when you drag an icon into
the trash can that icons disappears from your
array of JLabels.
Hint: You may want to create your
own subclass of the JLabels so that a JLabel will contain data for its position in the
array. You will need this to shift
the values around in the array once a label is deleted.
- Modify LabelTransferHandler
so that you can drag the icon from one label to another and it will insert
the dragged icon in that new position rather than just copying it there.
- Send your completed project
folder to jzimmerm@goucher.edu in zipped format for grading.