• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

How to simulate(define) a mouse dragged event

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have defined a component extends from JPanel, and I added a mouseMotionListener which implements mouseDragged method for it. Now, I want to define (new) a mouse dragged event for this component so that it can act like being dragged, instead of really using mouse. I have search MouseEvent class in Oracle.com, but I'm not able to define a mouse dragged event myself, Could you give me some ideas?
 
Rancher
Posts: 3051
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cross posted: https://stackoverflow.com/questions/46430920/how-to-automatically-simulatedefine-a-mouse-dragged-event#comment79850407_46430920

A suggestion has already been given. The OP has made no effort to implement the suggestion.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Camick wrote:Cross posted: https://stackoverflow.com/questions/46430920/how-to-automatically-simulatedefine-a-mouse-dragged-event#comment79850407_46430920

A suggestion has already been given. The OP has made no effort to implement the suggestion.



Thanks for your help mate, your tip was usefull for me aswell!
 
Marshal
Posts: 64680
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bella Abakumova welcome to the Ranch
 
Saloon Keeper
Posts: 3300
146
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A simple way to "drag" some component, without using the mouse and not using the Robot class, is tp play with that components' bounds. A simple demonstration:

I am not using a NullLayout here. Resize the frame (by dragging one of its sides) to see what that means! But it is just for demonstrationpurposes.

Now, since I have no idea what your intentions are, this demo might be worthless to you. Can you explain what it is that you are trying to achieve?
 
Jose Ariza
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the answer, Actually I add some link(line) to my custom view, but when adding some lines may disappear, but I found when I drag my view ,the line comes visiable. So I want to achieve a mouse dragged event by program instead of mouse.
I have a simple example program here, The Rect Class is the custom View which already achieved a mouse dragged method, so you can drag it on the frame. MainFrame class is just a frame contains those rect.



All I want to achieve is may be when something happend (resizing the window or click on the frame) the rect can act like been dragged.
 
Jose Ariza
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have seen your works, it simluate dragged event by setLocation, I try your code in my program and i found only set location is not enough, the component must act like being dragged, it means i should simulate a pressed event ,move event,released event to the component, and this is my difficult point.
 
Jose Ariza
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have achieved this by Robot class,Here's my key code(really simple...)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!