Home Java Example Java Swing Making a component drag gable in java
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Making a component drag gable in java

Advertisement
In this section, you will learn how to make a component draggable in java.

Making a Component Draggable in Java

     

In this section, you will learn how to make a component draggable in java. This program provides the drag and drop feature from one component to another. This program shows a text box and a label on the frame. When you drag the label and drop to the text box then the text of the label is copied to the text box. For the pictorial representation of the application result is given as follows:

Drag And Drop

Code Description:

There are several APIs has been used to make the component draggable in the program. These are explained as follows:

TransferHandler:
This is the class of the javax.swing.*; package. This is used to transfer the transferable swing component from on the another. Both components should be the swing component. This class is helpful to copy from one component to another via clipboard.

setTransferHandler():
This is the method of the TransferHandler class which makes the component able to transfer the specified part of the component in the
TransferHandler() as parameter which specifies the the property of the component which has to be dragged and dropped. This program specifies the text of the label to be dragged and drop to the text box using the string "text".

MouseListener:
This is the interface which receives the different mouse events. Mouse events can be like: pressing or releasing the mouse, click, enter or exit the mouse etc. are handled through the MouseEvent generated by the MouseListener interface. This program uses the instance of the MouseListener.

exportAsDrag():
This is the method of the TransferHandler class which initiates the component to drag and drop from one to another swing component. This method takes three arguments like: JComponent, Active event, and another is the Action which has to be done. Here, the constant property COPY of the TransferHandler class has been used to copy the text from the label to the text box.

Here is the code of the program: 

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SwingDragDrop{
  JTextField txtField;
  JLabel lbl;
  public static void main(String[] args){
  SwingDragDrop sdd = new SwingDragDrop();
  }

  public SwingDragDrop(){
  JFrame frame = new JFrame("Drag Drop Demo");
  txtField = new JTextField(20);
  lbl = new JLabel("This is the text for drag and drop.");
  lbl.setTransferHandler(new TransferHandler("text"));
  MouseListener ml = new MouseAdapter(){
  public void mousePressed(MouseEvent e){
  JComponent jc = (JComponent)e.getSource();
  TransferHandler th = jc.getTransferHandler();
  th.exportAsDrag(jc, e, TransferHandler.COPY);
  }
  };
  lbl.addMouseListener(ml);
  JPanel panel = new JPanel();
  panel.add(txtField);
  frame.add(lbl, BorderLayout.CENTER);
  frame.add(panel, BorderLayout.NORTH);
  frame.setSize(400400);
  frame.setVisible(true);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setResizable(false);
  }
}

Download this example.

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 14, 2007

Ask Questions?    Discuss: Making a component drag gable in java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Abhinav
September 13, 2011
making drag n drop tool

pls send me the code for making drag n drop tool in java
Abhinav
September 13, 2011
making drag n drop tool

is it easy to make drag drop project in Swing compare with JSP?
pooja
September 11, 2012
complain

Your outputs are not apt..... just waste of time.............
DMCA.com