Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Making a component drag gable in java 
 

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.

                         

» View all related tutorials
Related Tags: c com image class list object diff components stack icons user sed entity icon order component ai define container show

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.