GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes How is a Graphical object move on the Screen by using JButton. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How is a Graphical object move on the Screen by using JButton." Watch "How is a Graphical object move on the Screen by using JButton." New topic
Author

How is a Graphical object move on the Screen by using JButton.

Udara Amarasinghe
Ranch Hand

Joined: Aug 17, 2009
Posts: 109

How is a Graphical object move on the Screen by using JButton until the button release.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
Welcome to JavaRanch

We don't simply give out such answers, but if you would show us what you have so far, we shall be happy to comment on it.
By the way: it sounds very unusual behaviour for a button to start motion, then stop it when the mouse is released.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10426
    
    8

What you have in mind is not possible with the action listener.
An action performed will not be fired until a successful click.
Click=press+release.

Check out the mouse listeners.
Like Campbell said, try out various scenarios on your own. If you run into a problem, show us your code, and we will be more than happy to help you figure out what is going wrong.
Best of luck.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10426
    
    8

Udara,

Please read my previous reply carefully why it is not possible with an action listener.
In case you do not know about mouse listeners, here is a tutorial
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
Maneesh has already told you, you need a different Listener. You would also need a Timer (at the very least) or a new Thread to move the object. Have a look through the tutorial you have been told about.
Udara Amarasinghe
Ranch Hand

Joined: Aug 17, 2009
Posts: 109

I solved it with help of Campbell Ritchie and Maneesh Godbole comments.
This is the solved code.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
Well done It is far better to solve the problem yourself than be told a solution because you will remember the answer better.
 
GeeCON Prague 2014
 
subject: How is a Graphical object move on the Screen by using JButton.