Drop And Drag An Icon In Java | Free Source Code

Drop and Drag an Icon in Java

June 13, 2020
Tutorials
drag and drop an icon in java

In this tutorial, we will teach you how to make a drag and drop an Icon in java.

So, now let’s start this tutorial!

1. Open JCreator or NetBeans and create a java program with a file name of dragDropIcon.java.

2. Import the following package library:

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

3. Create a class name DragMouseEvent that will extend a MouseAdapter to access a component so that it can drag. Use the JComponent class so that it will call the component and the TransferHandler class to have a TransferHandler.COPY constant of the component.

class DragMouseEvent extends MouseAdapter {
  public void mousePressed(MouseEvent e) {
    JComponent c = (JComponent) e.getSource();
    TransferHandler handler = c.getTransferHandler();
    handler.exportAsDrag(c, e, TransferHandler.COPY);
  }
}

4. We will initialize variables in our Main, variable frame as JFrame, icon1icon2icon3 as ImageIcon (you can have any image files on this), button1 as JButton, label1 and label2 as JLabel.

JFrame frame = new JFrame("Drag Drop Icon");
ImageIcon icon1 = new ImageIcon("1.gif");
ImageIcon icon2 = new ImageIcon("2.gif");
ImageIcon icon3 = new ImageIcon("3.gif");
 
JButton button = new JButton(icon2);
 
JLabel label1 = new JLabel(icon1, JLabel.CENTER);
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.

MouseListener listener = new DragMouseEvent();

Then add the MouseListener class to the labels with the use of addMouseListener method.

label1.addMouseListener(listener);
label2.addMouseListener(listener);

To have all the components to be transferable, we will use the setTransferHandler method of all the components.

label1.setTransferHandler(new TransferHandler("icon"));
button.setTransferHandler(new TransferHandler("icon"));
label2.setTransferHandler(new TransferHandler("icon"));

6. We will have its layout into Grid Layout using the setLayout method of the frame.

frame.getContentPane().setLayout(new GridLayout(0,1));

Add all the buttons and labels to the frame using the add method.

frame.getContentPane().add(label1);
frame.getContentPane().add(button);
frame.getContentPane().add(label2);

Lastly, set the size, visibility, and the close operation of the frame. Have this code below:

frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);


Here’s the full code of this tutorial:

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
 
class DragMouseEvent extends MouseAdapter {
  public void mousePressed(MouseEvent e) {
    JComponent c = (JComponent) e.getSource();
    TransferHandler handler = c.getTransferHandler();
    handler.exportAsDrag(c, e, TransferHandler.COPY);
  }
}
 
public class dragDropIcon {
  public static void main(String[] args) {
 
    JFrame frame = new JFrame("Drag Drop Icon");
    ImageIcon icon1 = new ImageIcon("1.gif");
    ImageIcon icon2 = new ImageIcon("2.gif");
    ImageIcon icon3 = new ImageIcon("3.gif");
 
    JButton button = new JButton(icon2);
 
    JLabel label1 = new JLabel(icon1, JLabel.CENTER);
    JLabel label2 = new JLabel(icon3, JLabel.CENTER);
 
    MouseListener listener = new DragMouseEvent();
 
 
    label1.addMouseListener(listener);
    label2.addMouseListener(listener);
 
    label1.setTransferHandler(new TransferHandler("icon"));
    button.setTransferHandler(new TransferHandler("icon"));
    label2.setTransferHandler(new TransferHandler("icon"));
 
    frame.getContentPane().setLayout(new GridLayout(0,1));
    frame.getContentPane().add(label1);
    frame.getContentPane().add(button);
    frame.getContentPane().add(label2);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }
}

 

Facebook Comments

Leave a Reply

Your email address will not be published.