Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training  

adding mouse listeners to drop target



Share on Google+Share on Google+

adding mouse listeners to drop target
2 Answer(s)      5 years ago
Posted in : Java Beginners

import java.awt.*; import java.awt.dnd.DnDConstants; import java.awt.dnd.DragGestureRecognizer; import java.awt.dnd.DragSource; import java.awt.dnd.DropTarget; import javax.swing.*; import javax.swing.border.Border; import javax.swing.border.TitledBorder; import javax.swing.JComponent.*; import javax.swing.table.*; import java.awt.event.*; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem;

import java.awt.geom.*; import javax.swing.border.TitledBorder; //import java.awt.dnd.DropTargetEvent.*;

public class JPanels extends JFrame { private static ActionListener actionListener; public static void main(String[] args) {

new JPanels();


public JPanels() {

JFrame frame = new JFrame("DND INTERFACE"); frame.setDefaultCloseOperation(JFrame.EXITONCLOSE); JMenuBar menuBar = new JMenuBar();

JMenu fileMenu = new JMenu("File");

JMenuItem newMenuItem = new JMenuItem("New", KeyEvent.VK_N);

frame.setSize(800, 500);

addWindowListener(new ExitListener());
Container content = getContentPane();

JPanel SixChoicePanel1 = new JPanel (new GridLayout(15,1));


JButton option = new JButton("Drag this button1"); JButton option1 = new JButton("Drag this button"); JButton option2 = new JButton("Drag this button"); JButton option3 = new JButton("Drag this button"); JButton option4 = new JButton("Drag this button");



JPanel panel = new JPanel(new GridLayout(2,1));

JTable table; //=new JTable;

DefaultTableModel modeltable = new DefaultTableModel(8,8);

table = new JTable(modeltable); table.setBorder(BorderFactory.createLineBorder (, 2));

int height = table.getRowHeight(); table.setRowHeight(height=50);

table.setColumnSelectionAllowed(true); //table.setTransferHandler(new MyTransferHandler()); table.setDragEnabled(true);


panel.add(table); panel.setSize(400,400);

JPanel area = new JPanel();
JButton btnSave = new JButton("Save");


    frame.add(panel, BorderLayout.EAST);


    DnDListener dndListener = new DnDListener();
    DragSource dragSource = new DragSource();
    DropTarget dropTarget1 = new DropTarget(table, dndListener);

   DragGestureRecognizer dragRecognizer2 = dragSource.
          DnDConstants.ACTION_COPY, dndListener);
   DragGestureRecognizer dragRecognizer3 = dragSource.
            DnDConstants.ACTION_COPY, dndListener);

} }

i have a problem with adding mouse listeners to "table" which is drop target, to accept drop component wherever it drops from the mouse. in this code when component drops in to a drop target it always goes to a default position. i cant customize the position on drop target. please someone help me with this. thanks in advance

View Answers

July 10, 2012 at 11:29 AM

Here is a drag and drop application of Java Swing.

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) {
Object object;
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);
public void dragGestureRecognized(DragGestureEvent ev) {
ev.startDrag(null, this, this);
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) {
public void dragEnter(DropTargetDragEvent ev) {
public void dragExit(DropTargetEvent ev) {
public void dragOver(DropTargetDragEvent ev) {
public void dropActionChanged(DropTargetDragEvent ev) {
void dropTargetDrag(DropTargetDragEvent ev) {
public void drop(DropTargetDropEvent ev) {
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();
catch (Exception ex) {

July 10, 2012 at 11:30 AM


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();

    Frame source = new Frame("Source Frame");
    source.setLayout(new FlowLayout());

    JFrame target = new JFrame("Target Frame");
    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);


For more information, visit the following link:


Related Tutorials/Questions & Answers:
adding mouse listeners to drop target
with adding mouse listeners to "table" which is drop target, to accept drop...adding mouse listeners to drop target  import java.awt.*; import... in to a drop target it always goes to a default position. i cant customize
how to custom location of a object after dragging to the drop target
;how to change the position of a button when after it drags on the drop target... how to drag the object inside the drop target. (that means how to change the position of the object after it drops on 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
Event Listeners
Event Listeners  I want to learn the Event Listeners in Java. Any... is the good tutorial and example of Event Listeners in Java. Please check the tutorial Java - Event Listeners Example in Java Applet. Thanks
Action Listeners
Action Listeners  Please, could someone help me with how to use action listeners I am creating a gui with four buttons. I will like to know how to apply the action listener to these four buttons.   Hello Friend, Try
Mouse Listener
Mouse Listener  how to move any object with help of mouse but not drag it in an applet
Mouse Motion
Mouse Motion  Could you please send me the code of finding the (x,y) co-ordinates in a JFrame Compoenent..?? I need it urgently for my project
Event listeners in mxml
Event listeners in mxml  hi.. just tell me about How do you add event listeners in mxml components. Now AS3 components? Please give an example in both MXML and AS3 Thanks  Ans: There are some function
mouse events
mouse events  When i click a button i want to retrieve image and audio from database and play it. how can i do dis. I m using Netbeans IDE and mysql for database. pls do hel[p me
how to use mouse pressed
how to use mouse pressed   to action mouse clicked
Image swap on mouse over and mouse move
Image swap on mouse over and mouse move  i tried the code , sent form u...But if there are 50 images and..i want swap images on mousse over and mouse out, then the code becomes very please give the code for multiple
Image swap on mouse over and mouse move
Image swap on mouse over and mouse move  i tried the code , sent form u...But if there are 50 images and..i want swap images on mousse over and mouse out, then the code becomes very please give the code for multiple
How to set target to videos??
How to set target to videos??  Hi, i am developing project in HTML 5 with videos so, i am dispalaing videos in two table coloumn,if the video in one coloumn is clicked,it should be played in another coloumn. plzz suggest any idea
mouse over effects in css
mouse over effects in css  I have written a mouse over effect in CSS. But it is not working in HTML. Can any one suggest me? Thanks
Adding an employee
Adding an employee  coding for adding an employee
Swap images oon mouse over and mouse out
Swap images oon mouse over and mouse out  I have multiple images...i want to swap each image on mouse out and mouse over..   <html> <script language="javascript" type="text/javascript"> if(document.images
HTML5 target attribute, Definition and use of target attribute.
HTML5 target attribute, Definition and use of target attribute. In this tutorial we will inform about the target attribute of <area> tag in html5... in the document. The target attribute helps to open the document in the same window
mouse event - Java Beginners
mouse event  import javax.swing.*; import java.awr.*; import...=getContentPane(); setLayout(new FlowLayout()); setTitle("Mouse Event..."); public MouseEventt() { f.setLayout(new FlowLayout()); f.setTitle("Mouse
4D Mouse
4D Mouse       Here... mouse is an advanced species of traditional 2D mouse that can move only horizontal no vertical, while 4D mouse can move all the direction ups & down and left
Drop Box
Drop Box  program draw 2d shapes in java
Drop Box
Drop Box  program draw 2d shapes in java
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
Handling Mouse Clicks in Java
Handling Mouse Clicks in Java     ... the mouse click event in the awt application. This program simply implements the left click event of the mouse. When you click "Click Me" button
drop down
drop down  how can i add data from choice/dropdown component of java awt to myaql table
Drop Down
Drop Down  How to insert date into database using dropdown like facebook
adding a dialogue
adding a dialogue  Blockquote Hi can you help with the program below,the program is a loop that prints out a code and a quantity when prompt for the user input.what I need is to modify the code to incorporate a dialogue asking
adding loop
adding loop  Hi I have a program that is not compiling when I add a loop can you help me?The program below is compiling without the loop. > Blockquote mport java.util.*; import java.text.*; import java.util.Scanner
How to design a mouse
How to design a mouse Follow this example to learn how to design a mouse. New File: First take a new file (Ctrl + N). Use Pen Tool (P... mouse pointer then select Direct selection tool (A key) and adjust the points
Reflection api :Invocation target exception - JDBC
Reflection api :Invocation target exception   Am using a function to insert a record into the database .Using reflection api am invoking... in Invocation target Exception .How to resolve
Target attribute in anchor tag, Use of target attribute of anchor tag in HTML5.
Target attribute in anchor tag, Use of target attribute of anchor tag in HTML5. In this tutorial, we will see the use of target attribute of anchor tag in HTML5. Target attribute tells the browser where to display linked document
Wats Deploying war in Target Server - Development process
Wats Deploying war in Target Server   Hi Friends, I am new to web application, can u explain briefly wat is "Deploying a .war,.jar,.ear files in target server. Thank u in advance   Hi rajendra