Question:
How do you drag a JLabel in java so that it will vanish when you drop it in another label or part of the scree?
needhelpasap
2009-03-28 23:27:17 UTC
n? I need the methods and interfaces that I might need. I have slightest idea on how to do this.
Three answers:
Mohamed Mansour
2009-03-29 00:22:59 UTC
Hey there,



Java has a drag and drop support for components within its awt package called dnd. You can read more about them here:

http://java.sun.com/docs/books/tutorial/dnd/index.html

There is Graphics2D, where you can draw each component manually, while overriding paint method, and figure out where the drop location is, but that requires you to do a lot of ugly work. Java DND is the answer to drag and droppable components.



It is a quite complex task if you are not familiar with Java. But if you are, the steps are really simple. Basically, you need to define what your Transferable object would be. In your case, your transferable object would be a JLabel which is just a plain text DataFlavor. That DataFlavor is needed to tell your drag and drop what is draggable.



Once you have created your Transferable object, a simple way to drag and drop a JLabel would be to implement DragGestureListener and DragSourceListener.



DragGestureListener is needed to inform it what to do when you start a drag, such as setting it what will be transferred via your Transferable Object. The only thing you need to implement there would be one function where you just start the drag.



public void dragGestureRecognized(DragGestureEvent dge) {

dge.startDrag(null, new JLabelTransferable(myLabel), this);

}



JLabelTransferable implements Transferable and describes the transfer.



Your DragSourceListener will define the state of the users gesture, and provide some nice feedback back to the user throughout the Drag and Drop operation.



Thats it, the example below will show you how to do a simple drag, you can extend it further to make it pretty. But please take a look at the Java documentation, it gives you many nice examples and teaches the concept well!



http://www.java2s.com/Code/Java/Swing-JFC/JLabelDragSource.htm



Good Luck
deonejuan
2009-03-29 08:08:59 UTC
Java does come with DnD. I never found any use for DnD. Instead, I use Graphics2D.



What you have with just drawing on a JPanel is a live picture with a mouseListener. Along with an ArrayList boxes;



The mouse clicks, the MyRectangle.contains(mx,my) boolean, the action deletes that Rectangle and adds it to the end of ArrayList boxes, whileMouseDown, repaint. Because the rectangle you are interested in is the last graphic, it is painted on top of everything else. Therefore, it looks like it is dragging.



This technique of collecting all the players and repaint the results is called SceneGraph. The ArrayList is built into an Animation loop as part of a software project. I am seeing more and more SceneGraph, so Objects from all parts of the java zoo can join the party.



It's a different way of thinking, this Java Graphics2D. DnD requires knowing all of the 'genetics' of the Swing JFC. I never liked the Cursor behavior with the Swing DnD.



I personally think my GUI is much more satisfactory just drawing the widgets I need, making the JComponents what I need after that.



If that appeals to you, get the free Java textbook download and read a couple of chapters.
2009-03-30 12:59:43 UTC
I would use Java DND, they made all the work easy for you to use. All you have to deal with is Telling it what to drag, and to do while dragging (such as show text, or make some noise), and what to do once you stop dragging (like make it colorized, etc). Very easy to do once you get your hands on it!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...