I try to get familar with drag and drop in java but all the tutorials I found were... (getting me angry)
All I want is to drag a "PublicUserLabel" from a JList (included in a selfmade JPanel called "UserPanel") and drop it in a selfmade class inerited from JTabbedPanel.
It is very important to drag the object itself and not its stringrepresentation!!!
That is what I have so far:
PublicUserLabel
public class PublicUserLabel extends JLabel implements DragSourceListener, DragGestureListener, Transferable
{
private DragSource ds;
private PublicUser user;
public PublicUserLabel(PublicUser user)
{
super(user.getName());
this.user = user;
ds = new DragSource();
ds.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY_OR_MOVE, this);
}
@Override
public void dragGestureRecognized(DragGestureEvent e)
{
ds.startDrag(e, DragSource.DefaultCopyDrop, this, this);
}
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException
{
if (flavor.equals(PublicUserFlavor.publicUserFlavor))
{
return this;//TODO ?
}
throw new UnsupportedFlavorException(flavor);
}
@Override
public DataFlavor[] getTransferDataFlavors()
{
DataFlavor[] df = new DataFlavor[2];
df[0] = DataFlavor.stringFlavor;
df[1] = PublicUserFlavor.publicUserFlavor;
return df;
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor)
{
return flavor.equals(PublicUserFlavor.publicUserFlavor);
}
//some more methods
}
UserPanel:
public class UserPanel extends JPanel
{
private JTextField search;
private List<PublicUser> allUser;
private JList<PublicUserLabel> list;
private JScrollPane scrollPane;
private DefaultListModel<PublicUserLabel> listModel;
public UserPanel()
{
allUser = new LinkedList<PublicUser>();
listModel = new DefaultListModel<PublicUserLabel>();
list = new JList<PublicUserLabel>(listModel);
list.setDragEnabled(true);
PublicUserFlavor:
public class PublicUserFlavor extends DataFlavor
{
public static DataFlavor publicUserFlavor;
static
{
publicUserFlavor = new DataFlavor(PublicUser.class, "PublicUser");
}
}
TabPanel:
public class TabPanel extends JTabbedPane implements DropTargetListener
{
public TabPanel()
{
setTabPlacement(JTabbedPane.BOTTOM);
addNewTabComponent("bla");
addNewTabComponent("blub");
setDropTarget(new DropTarget(this, this));
}
@Override
public void drop(DropTargetDropEvent e)
{
Transferable transferable = e.getTransferable();
if (transferable.isDataFlavorSupported(PublicUserFlavor.publicUserFlavor))
{
e.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
try
{
Object o = transferable.getTransferData(PublicUserFlavor.publicUserFlavor);
System.out.println(o);
if (o instanceof PublicUserLabel)
{
PublicUserLabel l = (PublicUserLabel)o;
PublicUser u = l.getUser();
System.out.println(u);
}
}
catch (UnsupportedFlavorException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
catch (IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.getDropTargetContext().dropComplete(true);
}
}
In the drop method are some syso's that shall be executed if a User is droped in the panel. But that is not the fact. Am I doing something completly wrong?
Thank you for helping!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…