| |
Struts Tutorials:
Struts2 Tag CheckBoxList , Checkbox, Iterator, IF
Struts2 Tag Library Example
Struts2 Tiles Example
Struts2 Tiles I18N Example
Struts2 Questions
Struts Tiles I18N Example
Struts Eclipse MVC
Struts2 Tags
Struts2 Example and Tutorial
Struts MVC
Struts2 Validation
Hibernate Tutorials:
Hibernate Case Study
Class Hierarchy Persist Example
Using Hibernate Interceptor
Hibernate Questions with Answer
Hibernate Many-to-Many Mapping Example
Hibernate one-to-many Mapping Example
Hibernate and ORM tools
Spring Hibernate Example
Hibernate SessionFactory Example
Hibernate Mapping Class Hierarchy
Hibernate Questions
Hibernate SessionFactory Questions
Spring Hibernate Example:
Spring Hibernate Case Study
Written By : Amit
Title :
Comparator and Reflection using Java
Description :
Java and CompareTo More...
|
Written By : Amit
Title :
Composite keys in Hashtable
Description :
key and value pair More...
|
Written By : Amit
Title :
Customizing JTree using Swing
Description :
AWT using Java Technology More...
|
Written By : Amit
Title :
Event handling and Java Technology
Description :
Event handling and Java Technology More...
|
Written By : admin
Title :
Using Final Keyword
Description :
Example on using Final Keyword in Java code More...
|
| Tags/Keywords : Using-Java-Technology - Example-of-Drag-and-Drop - Coding-Tips-on-Java Example Author : Amit Date (Year/Month/Date): 2009-01-27
Example of Drag and Drop using Java Technology | | | | |
|
Drag and Drop (DnD) using
Java Technology. A simple to
understand article to the
best of my understanding about
Java Technology Drag and Drop.
This writing is open for
discussion. I welcome all
suggestions/comments/ideas,
please write to me in this
page.
While creating Drag and Drop
functionality using Java API,
there are some of the basic
points, as follows:
1. At least one DragSource,
instantiated/associated with
the component that is to be
drag and drop.
|
2. At least one DropTarget that is to be instantiated
or associated with component that is used as the container
for the dropped component to reside.
3. Three main type of listeners, those will be notified as
and when particular event triggers while doing drag and drop
operation.
These are:
3.1. DragSourceListener, notifies when DragSourceEvent,
DragSourceDragEvent and DragSourceDropEvent trigger.
3.2. DragGestureListener, gets notification if
DragGestureEvent id triggered.
3.3. DropTargetListener, gets notification of DropTargetEvent,
DropTargetDragEvent, DropTargetDropEvent triggers.
4. The component that is to be dragged should create a
DragGestureRecognizer, simple way is to create a default
DragGestureRecognizer, by calling a method
DragSource.createDragGestureRecognizer method.
So we have three different areas to this explanation.
One -> The main frame on which both components, drag and drop
components.
Two -> The component to be dragged, for my example, I choose
java.awt.Button.
For this sample, I defined a customized, button that extends
java.awt.Button and implements two event listeners such as
DragSourceListener, DragGestureListener.
Three -> The component on top of it we are going to
drag this customized button, is a customized Panel.
This Panel extends java.awt.Panel and implements event
listener specific to DnD operation is DropTargetListener.
Now listeners are in place, and DragGesture recognizer ready
to recognizer and DragSource and DropTarget are ready to be
notified, but actual drag operation has to be started by
DragGestureRecognizer, in this case it is our customized
Button.
But one interesting point I found/observed that actually Button
is not moving even after startDrag operation is invoked.
I had to write a specific way to check for the x,y points
of mouse pointer/cursor during drag operation, extract dragSource
component and move it to the new location and place it on the
drop location.
This is one doubt I have, when I have to manually move drag
component, then I can actually do this by using mouse motion
and mouse listener,
Why I have to use Drag and Drop classes? You can reply your answers
here.
If interested in writing about this article, please do so
by sending an email to me. My email id is
"share.understanding at gmail.com".
Comments:
From: TAQI TAHA
Date: 12-March-2008
Comments:
Hello This is taha . I came across the code at
http://question2answer.googlepages.com/java-example-drag-n-drop.html
I am using java 6.
We could do the same thing like this for example
Add a frame (MainFrame)
Add a panel(MainPanel) on the frame
Add a button (MoveButton) on the panel
Add a MouseDragged event for the button and in the event
write the code as follows
MoveButton.setLocation( MainPanel.getMousePosition().x ,
MainPanel.getMousePosition().y );
Thats it that will allow you to drag and drop the button .
Now my question is when you try to drag the button the mouse
pointer goes to the (0,0) co-ordinate location of the button.
I want the mouse pointer to stay where i begin to drag .
The same is happening with your code as well . How could we
solve this .
I am waiting for your reply .
----------------------------------------------------------
Name: Author
Date: 12-March-2008
Comments:
Hi TAQI TAHA,
Thanks for suggesting something that is really interesting to
work on.
I think it can be solved by adjusting actual mouse click X and Y
points with the position of the upper left corner of this component.
I shall work on this and shall come up with a sample code and
provide it here.
In the mean time if you have any other way of doing it, please
let us know.
----------------------------------------------------------
From: Guddu
Date: 15-March-2008
Comment:
This is the solution I was trying to mention in my earlier
proposed solution:
In TestDragDrop constructor, put this code for getting mouse
clicked position:
testButton.addMouseListener(new java.awt.event.MouseAdapter(){
public void mousePressed(java.awt.event.MouseEvent me) {
Constant.xx = me.getX();
Constant.yy = me.getY();
}
public void mouseReleased(java.awt.event.MouseEvent me) {
Constant.xx = 0;
Constant.yy = 0;
}
});
Define two more constants in Constant class file as follows:
public static int xx;
public static int yy;
By changing setLocation method arguments (from dragOver method)
from setLocation(Constant.x, Constant.y);
to setLocation(Constant.x-xx;Constant.y-Constant.yy);
Strategy is to capture location of the mouse pressed event
to know exactly where the mouse pointer was present at the
time of mouse button is pressed.
Once the mouse pointer position/location is found, then it cane
be put in constant as xx and yy variables.
By subtracting this value from the dynamic value of x and y,
mouse pointer is shown at the desired location while dragging.
Please let me know if you have anything other solution and want
it to be discussed.
Thanks.
| |
|
| Are you interested in solving a very interesting Technology Stack while Playing this Game 
|
|
| Home >>> Coding Tips on Java >>> Example of Drag and Drop >>> Using Java Technology |
|
|
Visitor/User referred related external URL:
(Visible upon review and approved by this site Administrator)
|
|
|
|
|
<- requires login | Log in or Register | |
Copyright © 2008-2009, Interview-Questions-Tips-Forum, All Rights Reserved. | CONTACT PRIVACY POLICY DISCLAIMER |
 |
Terms of Use and Disclaimer :
This web site provides some of the information about various technologies, example
code, tips, tutorials etc. Like any printed materials, content of these pages may
become out of date over a period of time. Therefore all visitor/users of this web
site are requested/advised to refer to the originating parties/sources for the
latest changes and happenings for detailed information. This information is not
intended to be a substitute for the original reference provided by the originating
parties/sources.
By accessing and using this website in any ways, including, without
limitation, browsing the website pages, using any information, using any content and/or
downloading any materials, you agree to and are bound by the terms of use
described in this page and Usage Terms and Conditions.
If you do not agree to all of
the terms and conditions contained in the terms of use described in this
page and Usage Terms and Conditions, do not use this
website in any manner. If you are using the website on behalf of your
employer, you represent that you are authorized to accept these Terms of Use
on your employer's behalf.
All Trademarks are property of their respective owner. Appropriate measure is being
taken for providing accurate and up-to-date information but like any printed materials,
these blog(s)/contents may eventually be outdated one day, so if you are using any
of these information, please refer original content/documentation from respective sources.
And under no circumstances shall the Author of these contents and/or this web site
be liable for any loss, damage, expense incurred or suffered which is claimed to have
occurred because of usage of the contents of this web site.
If you have any questions/queries/feedback/suggestions then please write to this web
site owner at contact.
|  |
|
|
|
|
|