Drag and Drop an Icon in Java

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:
  1. import java.awt.*; // this is used to access the GridLayout class
  2. import java.awt.event.*; //this is used to access the MouseAdaptor, MouseEvent, and MouseListener class
  3. import javax.swing.*; //this is used to access JFrame, JLabel,JButton,JComponent, and TransferHandler class
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.
  1. class DragMouseEvent extends MouseAdapter {
  2.   public void mousePressed(MouseEvent e) {
  3.     JComponent c = (JComponent) e.getSource();
  4.     TransferHandler handler = c.getTransferHandler();
  5.     handler.exportAsDrag(c, e, TransferHandler.COPY);
  6.   }
  7. }
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.
  1.     JFrame frame = new JFrame("Drag Drop Icon");
  2.     ImageIcon icon1 = new ImageIcon("1.gif");
  3.     ImageIcon icon2 = new ImageIcon("2.gif");
  4.     ImageIcon icon3 = new ImageIcon("3.gif");
  5.  
  6.     JButton button = new JButton(icon2);
  7.  
  8.     JLabel label1 = new JLabel(icon1, JLabel.CENTER);
  9.     JLabel label2 = new JLabel(icon3, JLabel.CENTER);
5. To have a drag and drop on the icon components, we will instantiate the class named DragMouseEvent above.
  1.  MouseListener listener = new DragMouseEvent();
Then add the MouseListener class to the labels with the use of addMouseListener method.
  1.     label1.addMouseListener(listener);
  2.     label2.addMouseListener(listener);
To have all the components to be transferable, we will use the setTransferHandler method of all the components.
  1.     label1.setTransferHandler(new TransferHandler("icon"));
  2.     button.setTransferHandler(new TransferHandler("icon"));
  3.     label2.setTransferHandler(new TransferHandler("icon"));
5. Now, we will have its layout into Grid Layout using the setLayout method of the frame.
  1.     frame.getContentPane().setLayout(new GridLayout(0,1));
Add all the button and labels to the frame using the add method.
  1.     frame.getContentPane().add(label1);
  2.     frame.getContentPane().add(button);
  3.     frame.getContentPane().add(label2);
Lastly, set the size, visibility, and the close operation of the frame. Have this code below:
  1.     frame.pack();
  2.     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  3.     frame.setVisible(true);
Output: output Here's the full code of this tutorial:
  1. import java.awt.*; // this is used to access the GridLayout class
  2. import java.awt.event.*; //this is used to access the MouseAdaptor, MouseEvent, and MouseListener class
  3. import javax.swing.*; //this is used to access JFrame, JLabel,JButton,JComponent, and TransferHandler class
  4.  
  5. class DragMouseEvent extends MouseAdapter {
  6.   public void mousePressed(MouseEvent e) {
  7.     JComponent c = (JComponent) e.getSource();
  8.     TransferHandler handler = c.getTransferHandler();
  9.     handler.exportAsDrag(c, e, TransferHandler.COPY);
  10.   }
  11. }
  12.  
  13. public class dragDropIcon {
  14.   public static void main(String[] args) {
  15.        
  16.     JFrame frame = new JFrame("Drag Drop Icon");
  17.     ImageIcon icon1 = new ImageIcon("1.gif");
  18.     ImageIcon icon2 = new ImageIcon("2.gif");
  19.     ImageIcon icon3 = new ImageIcon("3.gif");
  20.  
  21.     JButton button = new JButton(icon2);
  22.  
  23.     JLabel label1 = new JLabel(icon1, JLabel.CENTER);
  24.     JLabel label2 = new JLabel(icon3, JLabel.CENTER);
  25.  
  26.     MouseListener listener = new DragMouseEvent();
  27.    
  28.    
  29.     label1.addMouseListener(listener);
  30.     label2.addMouseListener(listener);
  31.  
  32.     label1.setTransferHandler(new TransferHandler("icon"));
  33.     button.setTransferHandler(new TransferHandler("icon"));
  34.     label2.setTransferHandler(new TransferHandler("icon"));
  35.  
  36.     frame.getContentPane().setLayout(new GridLayout(0,1));
  37.     frame.getContentPane().add(label1);
  38.     frame.getContentPane().add(button);
  39.     frame.getContentPane().add(label2);
  40.     frame.pack();
  41.     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  42.     frame.setVisible(true);
  43.   }
  44. }
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

Add new comment