Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML


 
  
 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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.

                         

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 
Latest Searches:
list iteration in java
source code of sale
applet in dojotype
Scala Plugin
using JButton
for loop tutorial
how to user JSpinner t
Gimp Tutorials
java random numbers
composite key in mysql
multiplication of numb
facesmessage
combobox jsp
struts spring
get single character u
Photoshop Christmas Ba
what is the system.out
Photoshop Brushes Spar
log
Matrix
java code for id gener
components in JSP
html:checkbox is check
hibernate with logj4 l
create a directory and
http servlet
container managed pers
program in java to ins
my Sql server through
transact sql
displaytag
ignore
struts hibernates myec
utility of collections
how to load a frame us
myeclipse
Two Dimensional Array
how 2 comment a java l
Persistent Supplier ob
setvisible tab
struts html tag exampl
csv file format
Photoshop Digital Art
jfreechart
how to declare an arra
CMP Entity bean
user id js
retrieving file from
JCalendar
structs2 action
frame
rtsp
prime numbers between
3d max
mobile testing
dojo Spring
Custom Rendering of JL
Photoshop Effects Lake
previous
JSF panelGrid render
Filtering Selection fr
Read the Key-Value of
c:out in var of c:fore
draw triangle
contact
ListIterator
jdbctemplate.execute
code for i create a ta
clear values from a fo
selecting multiple fil
add picture to a swing
PHP Creating a Mail Fo
builb.xml
String to array in jav
covert uppercase to lo
how to use tab control
parse a int to string
save image to file
jboss portal
form bean update
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

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 | 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.