Drag and Drop an Icon in Java
Submitted by donbermoy on Friday, December 12, 2014 - 16:53.
This tutorial will teach you how to create a drag and drop an Icon in java.
So, now let's start this tutorial!
1. Open JCreator or NetBeans and make a java program with a file name of dragDropIcon.java.
2. Import the following package library:
3. Create a class name DragMouseEvent that will extend a MouseAdapter to access a component so that it can drag. We will use the JComponent class so that it will call the component and the TransferHandler class to have a TransferHandler.COPY constant of the component.
4. We will initialize variables in our Main, variable frame as JFrame, icon1, icon2, icon3 as ImageIcon (you can have any image files on this), button1 as JButton, label1 and label2 as JLabel.
5. To have a drag and drop on the icon components, we will instantiate the class named DragMouseEvent above.
Then add the MouseListener class to the labels with the use of addMouseListener method.
To have all the components to be transferable, we will use the setTransferHandler method of all the components.
5. Now, we will have its layout into Grid Layout using the setLayout method of the frame.
Add all the button and labels to the frame using the add method.
Lastly, set the size, visibility, and the close operation of the frame. Have this code below:
Output:
Here's the full code of this tutorial:
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.
Best Regards,
Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]
Add and Follow me on Facebook: https://www.facebook.com/donzzsky
Visit and like my page on Facebook at: https://www.facebook.com/BermzISware
- import java.awt.*; // this is used to access the GridLayout class
- import java.awt.event.*; //this is used to access the MouseAdaptor, MouseEvent, and MouseListener class
- import javax.swing.*; //this is used to access JFrame, JLabel,JButton,JComponent, and TransferHandler class
- TransferHandler handler = c.getTransferHandler();
- handler.exportAsDrag(c, e, TransferHandler.COPY);
- }
- }
- label1.addMouseListener(listener);
- label2.addMouseListener(listener);
- label1.setTransferHandler(new TransferHandler("icon"));
- button.setTransferHandler(new TransferHandler("icon"));
- label2.setTransferHandler(new TransferHandler("icon"));
- frame.getContentPane().add(label1);
- frame.getContentPane().add(button);
- frame.getContentPane().add(label2);
- frame.pack();
- frame.setVisible(true);

- import java.awt.*; // this is used to access the GridLayout class
- import java.awt.event.*; //this is used to access the MouseAdaptor, MouseEvent, and MouseListener class
- import javax.swing.*; //this is used to access JFrame, JLabel,JButton,JComponent, and TransferHandler class
- TransferHandler handler = c.getTransferHandler();
- handler.exportAsDrag(c, e, TransferHandler.COPY);
- }
- }
- public class dragDropIcon {
- label1.addMouseListener(listener);
- label2.addMouseListener(listener);
- label1.setTransferHandler(new TransferHandler("icon"));
- button.setTransferHandler(new TransferHandler("icon"));
- label2.setTransferHandler(new TransferHandler("icon"));
- frame.getContentPane().add(label1);
- frame.getContentPane().add(button);
- frame.getContentPane().add(label2);
- frame.pack();
- frame.setVisible(true);
- }
- }
Add new comment
- 388 views