adding mouse listeners to drop target

adding mouse listeners to drop target

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");
fileMenu.setMnemonic(KeyEvent.VK_F);
menuBar.add(fileMenu);


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

frame.setJMenuBar(menuBar);
frame.setSize(800, 500);
frame.setVisible(true);



WindowUtilities.setNativeLookAndFeel();
addWindowListener(new ExitListener());
Container content = getContentPane();
content.setBackground(Color.lightGray);

JPanel SixChoicePanel1 = new JPanel (new GridLayout(15,1));
setBackground(Color.lightGray);

SixChoicePanel1.setBorder(BorderFactory.createTitledBorder("Tools"));

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

  SixChoicePanel1.add(option);

  SixChoicePanel1.add(option1);
  SixChoicePanel1.add(option2);
  SixChoicePanel1.add(option3);
  SixChoicePanel1.add(option4);





JPanel panel = new JPanel(new GridLayout(2,1));
panel.setBackground(Color.white);

JTable table; //=new JTable;

DefaultTableModel modeltable = new DefaultTableModel(8,8);

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

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

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

le1.setFillsViewportHeight(true);

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

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


area.add(btnSave);

 panel.add(area);
frame.add(SixChoicePanel1);
    frame.add(panel, BorderLayout.EAST);

pack();



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

   DragGestureRecognizer dragRecognizer2 = dragSource.
            createDefaultDragGestureRecognizer(option1, 
          DnDConstants.ACTION_COPY, dndListener);
   DragGestureRecognizer dragRecognizer3 = dragSource.
            createDefaultDragGestureRecognizer(option2, 
            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) {
ex.printStackTrace();
}
}
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) {
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);
}

July 10, 2012 at 11:30 AM

continue..

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

For more information, visit the following link:

http://www.roseindia.net/java/example/java/swing/SwingDragDrop.shtml









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
ModuleNotFoundError: No module named 'odoo10-addon-mail-drop-target'
ModuleNotFoundError: No module named 'odoo10-addon-mail-drop-target'  ...: No module named 'odoo10-addon-mail-drop-target' How to remove the ModuleNotFoundError: No module named 'odoo10-addon-mail-drop-target' error
Advertisements
ModuleNotFoundError: No module named 'odoo10-addon-web-drop-target'
ModuleNotFoundError: No module named 'odoo10-addon-web-drop-target'  ...: No module named 'odoo10-addon-web-drop-target' How to remove the ModuleNotFoundError: No module named 'odoo10-addon-web-drop-target' error
ModuleNotFoundError: No module named 'odoo10-addon-web-drop-target'
ModuleNotFoundError: No module named 'odoo10-addon-web-drop-target'  ...: No module named 'odoo10-addon-web-drop-target' How to remove the ModuleNotFoundError: No module named 'odoo10-addon-web-drop-target' error
ModuleNotFoundError: No module named 'odoo11-addon-mail-drop-target'
ModuleNotFoundError: No module named 'odoo11-addon-mail-drop-target'  ...: No module named 'odoo11-addon-mail-drop-target' How to remove the ModuleNotFoundError: No module named 'odoo11-addon-mail-drop-target' error
ModuleNotFoundError: No module named 'odoo11-addon-web-drop-target'
ModuleNotFoundError: No module named 'odoo11-addon-web-drop-target'  ...: No module named 'odoo11-addon-web-drop-target' How to remove the ModuleNotFoundError: No module named 'odoo11-addon-web-drop-target' error
ModuleNotFoundError: No module named 'odoo12-addon-mail-drop-target'
ModuleNotFoundError: No module named 'odoo12-addon-mail-drop-target'  ...: No module named 'odoo12-addon-mail-drop-target' How to remove the ModuleNotFoundError: No module named 'odoo12-addon-mail-drop-target' error
ModuleNotFoundError: No module named 'odoo12-addon-mail-drop-target'
ModuleNotFoundError: No module named 'odoo12-addon-mail-drop-target'  ...: No module named 'odoo12-addon-mail-drop-target' How to remove the ModuleNotFoundError: No module named 'odoo12-addon-mail-drop-target' error
ModuleNotFoundError: No module named 'odoo12-addon-web-drop-target'
ModuleNotFoundError: No module named 'odoo12-addon-web-drop-target'  ...: No module named 'odoo12-addon-web-drop-target' How to remove the ModuleNotFoundError: No module named 'odoo12-addon-web-drop-target' error
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
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
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
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 lengthy...so 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 lengthy...so please give the code for multiple
ModuleNotFoundError: No module named 'mouse'
ModuleNotFoundError: No module named 'mouse'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'mouse' How to remove the ModuleNotFoundError: No module named 'mouse'
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
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
ModuleNotFoundError: No module named 'supervisor-event-listeners'
ModuleNotFoundError: No module named 'supervisor-event-listeners'  Hi...: No module named 'supervisor-event-listeners' How to remove the ModuleNotFoundError: No module named 'supervisor-event-listeners' error? Thanks
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
Drop Box
Drop Box  program draw 2d shapes in java
Drop Box
Drop Box  program draw 2d shapes in java
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
ModuleNotFoundError: No module named 'mouse_droid'
ModuleNotFoundError: No module named 'mouse_droid'  Hi, My Python... 'mouse_droid' How to remove the ModuleNotFoundError: No module named 'mouse_droid' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'mouse_record'
ModuleNotFoundError: No module named 'mouse_record'  Hi, My Python... 'mouse_record' How to remove the ModuleNotFoundError: No module named 'mouse_record' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'remarkable-mouse'
ModuleNotFoundError: No module named 'remarkable-mouse'  Hi, My... named 'remarkable-mouse' How to remove the ModuleNotFoundError: No module named 'remarkable-mouse' error? Thanks   Hi, In your
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
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
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
Adding an employee
Adding an employee  coding for adding an employee
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
drop down menu
drop down menu  drop down menu using html
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..., point your mouse pointer then select Direct selection tool (A key) and adjust
ModuleNotFoundError: No module named 'mouse_joystick_interface'
ModuleNotFoundError: No module named 'mouse_joystick_interface'  Hi...: No module named 'mouse_joystick_interface' How to remove the ModuleNotFoundError: No module named 'mouse_joystick_interface' error? Thanks  
drop down list in Struts
drop down list in Struts  How to create a drop down list in Struts
DROP a table and TRUNCATE
DROP a table and TRUNCATE   What are the differences between DROP a table and TRUNCATE a table
Drop Table
Drop Table       Drop Table in SQL get rid of an object or table from the database. Using a drop Query... from database. The Drop table cannot get it back, once you run the drop Query
ModuleNotFoundError: No module named 'drop'
ModuleNotFoundError: No module named 'drop'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'drop' How to remove the ModuleNotFoundError: No module named 'drop' error
Drop Index
Drop Index       Drop.... Understand with ExampleADS_TO_REPLACE_1 The Tutorial illustrate an example from Drop..._table(stu_id); Drop Index THE drop index stu_index 1 is used
ModuleNotFoundError: No module named 'target'
ModuleNotFoundError: No module named 'target'  Hi, My Python... 'target' How to remove the ModuleNotFoundError: No module named 'target'... to install padas library. You can install target python with following command
ModuleNotFoundError: No module named 'target'
ModuleNotFoundError: No module named 'target'  Hi, My Python... 'target' How to remove the ModuleNotFoundError: No module named 'target'... to install padas library. You can install target python with following command
ModuleNotFoundError: No module named 'target'
ModuleNotFoundError: No module named 'target'  Hi, My Python... 'target' How to remove the ModuleNotFoundError: No module named 'target'... to install padas library. You can install target python with following command
Auto select cell on mouse hover using 'hover' mouse event
Auto select cell on mouse hover using 'hover' mouse event In this tutorial, we will discuss about how to highlight Table's cell on mouse hover using jQuery 'hover' mouse event. In this example, a table is given

Ads