Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

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?

n? I need the methods and interfaces that I might need. I have slightest idea on how to do this.

3 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    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....

    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/JLabelDr...

    Good Luck

    Source(s): Software Engineer II Microsoft Student Partner
  • 1 decade ago

    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<MyRectangles> 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.

  • Anonymous
    1 decade ago

    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!

Still have questions? Get your answers by asking now.