Home Answers Viewqa Swing-AWT Java Swing Drag drop

 
 


som
Java Swing Drag drop
2 Answer(s)      4 years and 9 months ago
Posted in : Swing AWT

View Answers

September 3, 2008 at 3:03 PM


Hi friend,

import java.awt.*;
import java.awt.datatransfer.*;

import java.awt.dnd.*;
import java.awt.dnd.peer.*;
import java.awt.dnd.DropTarget;
import javax.swing.*;


public class DragDrop implements DragGestureListener, DragSourceListener,
DropTargetListener, Transferable {

static final DataFlavor[] supportedFlavors = {null};

static {
try {
supportedFlavors[0] = new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType);
} catch (Exception ex) {
ex.printStackTrace();
}
}
Object object;
// Transferable methods.
public Object getTransferData(DataFlavor flavor) {
if (flavor.isMimeTypeEqual(DataFlavor.javaJVMLocalObjectMimeType)) {
return object;
} else {
return null;
}
}

public DataFlavor[] getTransferDataFlavors() {
return supportedFlavors;
}

public boolean isDataFlavorSupported(DataFlavor flavor) {

return flavor.isMimeTypeEqual(DataFlavor.javaJVMLocalObjectMimeType);
}
// DragGestureListener method.
public void dragGestureRecognized(DragGestureEvent ev) {
ev.startDrag(null, this, this);
}
// DragSourceListener methods.
public void dragDropEnd(DragSourceDropEvent ev) {
}

public void dragEnter(DragSourceDragEvent ev) {
}

public void dragExit(DragSourceEvent ev) {
}

public void dragOver(DragSourceDragEvent ev) {
object = ev.getSource();
}

public void dropActionChanged(DragSourceDragEvent ev) {
}
// DropTargetListener methods.
public void dragEnter(DropTargetDragEvent ev) {
}

public void dragExit(DropTargetEvent ev) {
}

public void dragOver(DropTargetDragEvent ev) {
dropTargetDrag(ev);
}

public void dropActionChanged(DropTargetDragEvent ev) {
dropTargetDrag(ev);
}

void dropTargetDrag(DropTargetDragEvent ev) {
ev.acceptDrag(ev.getDropAction());
}

public void drop(DropTargetDropEvent ev) {
ev.acceptDrop(ev.getDropAction());
try {
Object target = ev.getSource();
Object source = ev.getTransferable().getTransferData(supportedFlavors[0]);

Component component = ((DragSourceContext) source).getComponent();

Container oldContainer = component.getParent();
Container container = (Container) ((DropTarget) target).getComponent();
container.add(component);
oldContainer.validate();
oldContainer.repaint();
container.validate();
container.repaint();
}
catch (Exception ex) {
ex.printStackTrace();
}
ev.dropComplete(true);
}

September 3, 2008 at 3:05 PM


public static void main(String[] arg) {
Button button = new Button("Drag this button");
Label label = new Label("Drag this label");
Checkbox checkbox = new Checkbox("Drag this check box");
CheckboxGroup radiobutton = new CheckboxGroup();
Checkbox checkbox1 = new Checkbox("Drag this check box", radiobutton, false);
Choice country = new Choice();

// adding possible choices
country.add("India");
country.add("US");
country.add("Australia");

Frame source = new Frame("Source Frame");
source.setLayout(new FlowLayout());
source.add(button);
source.add(label);
source.add(checkbox);
source.add(checkbox1);
source.add(country);

JFrame target = new JFrame("Target Frame");
target.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
target.setLayout(new FlowLayout());

DragDrop dndListener = new DragDrop();


DragSource dragSource = new DragSource();
DropTarget dropTarget1 = new DropTarget(source, DnDConstants.ACTION_COPY,dndListener);

DropTarget dropTarget2 = new DropTarget(target, DnDConstants.ACTION_COPY,dndListener);

DragGestureRecognizer dragRecognizer1 = dragSource.createDefaultDragGestureRecognizer(button, DnDConstants.ACTION_COPY, dndListener);
DragGestureRecognizer dragRecognizer2 = dragSource.createDefaultDragGestureRecognizer(label, DnDConstants.ACTION_COPY, dndListener);
DragGestureRecognizer dragRecognizer3 = dragSource.createDefaultDragGestureRecognizer(checkbox, DnDConstants.ACTION_COPY, dndListener);
DragGestureRecognizer dragRecognizer4 = dragSource.createDefaultDragGestureRecognizer(checkbox1, DnDConstants.ACTION_COPY, dndListener);
DragGestureRecognizer dragRecognizer5 = dragSource.createDefaultDragGestureRecognizer(country, DnDConstants.ACTION_COPY, dndListener);

source.setBounds(0, 200, 200, 200);
target.setBounds(220, 200, 200, 200);

target.setVisible(true);
source.setVisible(true);
}
}
-------------------------------------------

Thanks.









Related Pages:
Java Swing Drag drop - Swing AWT
Java Swing Drag drop  Hi i have following code that can help to drag abd drop , The code is also from Roseindia,net I want to keep orignal componant on frame after drag please help me import java.awt.*; import
javascript drag drop treeview - Ajax
javascript drag drop treeview  Hello Friends, I want to create...(); }   Drag Drop Example   Select any part of this text box to drag Drag <==> Drop
Java Swing drag image
Java Swing drag image Java Swing provides several classes that will allows us to drag and drop components(drop down list, text area, check box, radio button... to drag an image from one panel to another. For this, we have used two images. One
Drag and Drop Example in SWT
Drag and Drop Example in SWT       Drag and Drop in Java - This section is going to illustrates you how to create a program to drag and drop the tree item in Java . In SWT
Drag and Drop using java - Java Beginners
Drag and Drop using java   In this program the component location...()); } public void drop(DropTargetDropEvent ev) { ev.acceptDrop... static void main(String[] arg) { Button button = new Button("Drag
how to use javascript drag and drop files upload - Ajax
how to use javascript drag and drop files upload  Hi,Sir Please, I would like to upload multiple files using javascript with drag and drop style.I... for upload files in drag and drop style instead of using javascript.But I need
Ajax drag and drop
Ajax drag and drop  Hi, How to develop Ajax drag and drop example... and Droppable jQuery UI Interaction to perform drag and drop operations. In the above urls you will find the example of Ajax drag and drop operations. Thanks
Implement Drag and drop
Implement Drag and drop  Hi...... Please give the answer with example How do you implement drag and drop on components that do not support ondrag and ondrop? Thanks in advance   Ans: The example
Drag and drop file uploading - Ajax
Drag and drop file uploading  Hi all, This is NageswaraRao i want file uploading feature on my web development..using drag and drop mouse functionality. Problem:I have Created one Text area when i drop the file on text area
drag n drop - JSP-Servlet
drag n drop  I want to implement drag n drop functionality for simple HTML/JSP without using applet,flash or any heavy components.using browse button... using drag and drop mode.when user drag n drop file then display the complete path
Drag and Drop file upload in DOJO
Drag and Drop file upload in DOJO  Hi, We are working with DOJO for creating the UI for the web application.Our requirement is "Drag and Drop files from desktop to the browser for upload". Is this feature supported in DOJO? Any
Dojo drag and drop
Dojo drag and drop         In this section, you will learn about dojo drag and drop. Drag and Drop: This is a technique of dragging an item. Click an object
Drag and Drop components from one frame to another frame
Drag and Drop components from one frame to another frame... that how to drag and drop component (drop down list, text area, check box, radio...;One frame has five component, user can drag and drop component from one
Drag and Drop in Flex4
Drag and Drop in Flex4: Drag and Drop is the operation in which you can drag and drop an item or component in to another component. The drag and drop.... There are three stages of drag and drop operation which are following: 1. Initiation 2
adding mouse listeners to drop target
; Here is a drag and drop application of Java Swing. import java.awt.*; import...adding mouse listeners to drop target  import java.awt.*; import...")); JButton option = new JButton("Drag this button1"); JButton option1 = new
Flex Drag Drop Component
  Flex  Drag and Drop Component: The Flex 4 Drag and Drop is a process for selecting an item from a list or component and move on mouse pointer  and drop when release mouse pointer. Item are put from one position
how to custom location of a object after dragging to the drop target
/example/java/swing/drag-drop.shtml   no not like that, i just want to know how to drag the object inside the drop target. (that means how to change...how to custom location of a object after dragging to the drop target  
Mouse Drag and Drop
Mouse Drag and Drop       This section illustrates you how to drag and move mouse to draw a figure. To draw a figure using drag and drop, we have used Rectangle2D class to draw
Data Transfer
; Swing supports data transfer through drag and drop, copy-paste, cut-paste etc. Data transfer works between Swing components within an application... to transfer data. These are: 1. The diagram below displays the Drag and drop
Two way drag and drop in Flex`
  Flex Two Way Drag and Drop :- In this tutorial you can see two way drag and drop for list based controls. In this example we can set dragEnabled="...;  for both lists as you can see in this example. Two-way Drag and Drop
Drag and Drop in the same control in Flex4
Drag and Drop in the same control in Flex4: We use the three property dragEnabled, dropEnabled and dragMoveEnabled for performing drag and drop operation. In this example you can see how we can drag and drop the item in the same
Java Swing
Java Swing      ... table controls All AWT flexible components can be handled by the Java Swing... the basic user interface such as customizable painting, event handling, drag and drop
Sitemap Java Swing Tutorial
-to-One Relationship | JPA-QL Queries Java Swing Tutorial Section Java Swing Introduction | Java 2D API | Data Transfer in Java Swing | Internationalization in Java Swing | Localization | What is java swing
Making a component drag gable in java
draggable in java. This program provides the drag and drop feature from one... the component to drag and drop from one to another swing component. This method... drag the label and drop to the text box then the text of the label is copied
Java swings - Swing AWT
void main(String[] args) { JFrame f = new JFrame("Drag & Drop...Java swings  i have the following class .In that class i has two panels,panel1and panel2.panel1 contains an image.i want drag it in panel2(gray
Flex component move using drag and drop in Flex4
Flex component move using drag and drop in Flex4: In this example you can see how we can drag and drop the component from one place to another. There are the three classes of drag and drop operation: 1. DragManager: It manages
Drag Manager in flex
: The DragManager class manage drag and drop operation in which you can move...Drag Manager in flex  HI... Please tell me about What is a drag... control, and then drag it over another component to add it to that component
Flex Component Move with the help of drag and drop
Flex Button Drag and Drop with X and Y Co-ordinate :- In this tutorial you can  drag and drop for button with x and y co-ordinate or move one place to another in container. In this tutorial we can explain how to use Drag Manager
Copying and Moving data using drag and drop
Copying and Moving data using drag and drop: You can drag and drop the data...; <s:Label text="Drag and drop data from List to datagrid"... move data and add it to the drop target it will be removed from draginitiator
Drag and Drop between two list control in Flex4
Drag and Drop between two list control in Flex4: In this example you can see how the data items of list are drag and drop from one list to another list...([]); } ]]> </fx:Script> <s:Panel title="Drag
Drag Demo
Java NotesExample - DragDemo.java This is an example of DragDemo in Java applet.   You can test the example on this page.   Drag... /** DragDemo.java - Mouse drag example dual application/applet @author Fred Swartz
SWING
SWING  A JAVA CODE OF MOVING TRAIN IN SWING
Drop Box
Drop Box  program draw 2d shapes in java
Drop Box
Drop Box  program draw 2d shapes in java
Java Swing Tutorials
the JComboBox Component of swing in java. The JComboBox is used to display drop... Java Swing Tutorials       Java Swing tutorials -  Here you will find many Java Swing
drop down
drop down  how can i add data from choice/dropdown component of java awt to myaql table
swing
swing  Write a java swing program to delete a selected record from a table
flex didn't know how to drag and drop video and audio in flip book - Framework
flex didn't know how to drag and drop video and audio in flip book  hello sir in my project in flex flip book it is book which can be flip.our target to add the video audio and photo in this book i am able to add the photo
Drag from DIV overflow - Ajax
Drag from DIV overflow  Hello All, I have Two html DIVs to use. What i want to do is click and drag item from First DIV and drop in Second DIV. I use style sheet "overflow: auto" for both DIVs. So whenever i drag any item
Swing
Swing  Write a java swing program to search the details of the students. Searching is performed on studentā??s first name. The details of all those students having same name as in given in searching criterion will be displayed
Swing
Swing  Write a java swing program to search the details of the students. Searching is performed on studentā??s first name. The details of all those students having same name as in given in searching criterion will be displayed
Server-Side Swing Designer ULC Visual Editor
Server-Side Swing Designer ULC Visual Editor Build Rich Internet Applications visually using the ULC Visual Editor for Eclipse. Use this drag-and-drop GUI designer for UltraLightClient to develop rich client user interfaces
Specify the drag indicator by using the DragManager
;)] public var roseindiaImage:Class; // initialize the drag and drop...Specify the drag indicator by using the DragManager : In this example we must specify a size for the drag indicator. In this example we use a doDrag
java swing - Java Beginners
java swing  How to upload the image in using java swings. ex- we make a button to browsbutton and savebutton , when we click on the browsbutton , i... void drop(DropTargetDropEvent evt) { try { Transferable tr
how to drag top most images which is draw on applet.
how to drag top most images which is draw on applet.  Please help me, I have draw 83 images one by one from g.drawImages(,,,,); now I want to drag last image from 83 images and drop it suitable place, Now please help me , how can
Java Program - Swing AWT
Java Program  Write a Program that display JFileChooser that open a JPG Image and After Loading it Saves that Image using JFileChooser  Hi... drop(DropTargetDropEvent evt) { try { Transferable tr
java swing - Swing AWT
java swing  how i can insert multiple cive me exampleolumn and row in one JList in swing?plz g  Hi Friend, Please clarify your question. Thanks
Java swing
Java swing  what are the root classes of all classes in swing
Java swing
Java swing  Does Swing contains any heavy weight component
java swing
java swing  view the book details using swing

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.