Home Answers Viewqa Java-Beginners adding mouse listeners to drop target

 
 


cjay
adding mouse listeners to drop target
2 Answer(s)      10 months 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");
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 Pages:
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
Mouse Listeners
Java NotesMouse Listeners There are several styles for using the mouse listeners.   They are usually added to a graphics panel... a panel and want the listeners outside it because it is more convenient
The Mouse
- Handles moves and drags. Mouse Listeners - How and where to write mouse listeners... Java NotesThe Mouse The mouse is handled automatically by most components... (and shouldn't care) whether this was from a mouse click on the button, or from
Listeners
, and top-level listeners for mouse listeners for graphics panels. Other... listeners: Named Inner Class Listeners are one of the most common ways... with several components. Anonymous Inner Class Listeners are sometimes
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
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(ev.getDropAction()); try { Object target = ev.getSource
Java Swing Drag drop - Swing AWT
abd drop , The code is also from Roseindia,net I want to keep orignal...()); } public void drop(DropTargetDropEvent ev) { ev.acceptDrop(ev.getDropAction()); try { Object target = ev.getSource
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
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
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
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...()); } public void drop(DropTargetDropEvent ev) { ev.acceptDrop
jQuery '.live' & '.die' Mouse events
jQuery '.live'  & '.die' Mouse events...; mouse event of jQuery. In this Example, we removes the 'click' event... is for removing event and third button is the one whose event are adding &
Mouse Listener
Mouse Listener  how to move any object with help of mouse but not drag it in an applet
Drag and Drop Example in SWT
the target to drop the text. The method setTransfer() specifies the text that can... Drag and Drop Example in SWT       Drag and Drop in Java - This section is going to illustrates
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
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
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
Adding pop up menu on headlines.
Adding pop up menu on Headlines Here you will learn sticking a pop up menu on a title. We have added it on the product list. When the cursor of mouse goes on to pop up menu, it opens the list of the menu automatically. For generating
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
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
Drop down and radio button value on edit action
Drop down and radio button value on edit action  HI, I have a title field and a payment type field for title i have used drop down with values...; by cheque When 'm adding a new customer it works fine as we have to select
Adding an employee
Adding an employee  coding for adding an employee
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
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
Adding a Rollover and Pressed Icon to a JButton Component in Java
Adding a Rollover and Pressed Icon to a JButton Component in Java... about adding event i.e. the rollover and click icon to a JButton component of swing in java. Rollover means moving mouse pointer above the icon on the button. 
jQuery Simple Drop Down Menu
color="green"> Hover mouse on any menu item to drop down...jQuery Simple Drop Down Menu In this section, you will learn how to develop a simple drop down menu using jQuery. To develop a drop down menu we put
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
Copying and Moving data using drag and drop
move data and add it to the drop target it will be removed from draginitiator and show in drop target. When you copy data and add it to the drop target it will not be removed from draginitiator and it will be shown in the drop target. We
Using Ant Build Scripts to Drop Mysql Table
Using Ant Build Scripts to Drop Mysql Table       This example illustrates how to drop table... the password name of the database.  In this build.xml file, <target name="
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
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
Mouse Buttons, Modifier Keys
Java NotesMouse Buttons, Modifier Keys Mouse Buttons. Java supports up to three mouse buttons. Even if your mouse doesn't have three separate... the mouse button. The MouseEvent object that is passed to the listener
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
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
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
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
MouseTest
: mousedeme/MouseTest.java // Description: Main program/applet to demo mouse listeners...); this.setPreferredSize(new Dimension(200, 200)); //--- Add the mouse listeners... of listening to mouse events on a panel.   We have given the online
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

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.