*
The moose likes Game Development and the fly likes MouseMotionListener usage for Jbutton Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Game Development
Bookmark "MouseMotionListener usage for Jbutton" Watch "MouseMotionListener usage for Jbutton" New topic
Author

MouseMotionListener usage for Jbutton

Jack Weasels
Greenhorn

Joined: Jan 04, 2012
Posts: 22


// could you explain me how to code the functionality for the Jbutton to move on mouse event: drag....thanks
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Any suggestions ???

Any questions ???
Jack Weasels
Greenhorn

Joined: Jan 04, 2012
Posts: 22
HI,
I modified some code , can you explain how I should go about it.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

Jack, your question is very vague. Can you please explain more clearly what problem you're having?

Also, please UseCodeTags when you post code. People often don't even bother looking at unformatted code, myself included. I have added them for you this time.
Jack Weasels
Greenhorn

Joined: Jan 04, 2012
Posts: 22
Hi,
Thanks for tagging the code, can you try running my code and see that my JButton is not moving on dragging the mouse. I want to understand where am i going wrong.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

Probably because any attempt on your side to change the position of the component is being overridden by the LayoutManager of the JFrame's content pane.

Changing components positions in this way is usually a really bad idea. Why do you want to do this?
Jack Weasels
Greenhorn

Joined: Jan 04, 2012
Posts: 22
Thanks, issue resolved.
Jack Weasels
Greenhorn

Joined: Jan 04, 2012
Posts: 22

I thought I could resolve the issue by reading about repaint() but was unsuccessful.
Can you explain me how the background color "red" is resetting. I assume from the earlier reply that it is caused by the repaint method of jframe but could not find proper documentation on how it works, Have tried reading various examples available online but still couldn't figure out the repaint() working.
Also, please tell me how this statement works: "bar.addMouseMotionListener(this);", my intent is to make the button move on the frame with listener attached only to the jbutton if it is possible "at all".
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> my intent is to make the button move on the frame with listener attached only to the jbutton if it is possible "at all".

the button won't move whilst it is being controlled by a layout manager.
you need to set the layout to null, then you're responsible for size and location of the button.
in mouseDragged you need to get the mousePosition and the offset to the button's 0,0 for
button.setLocation(calc1,calc2)// not repaint() - setLocation will do that for you
Jack Weasels
Greenhorn

Joined: Jan 04, 2012
Posts: 22
OK, I just " setLayout(null); " before " new JButton() " in the constructor, but I still don't see the red background which is supposed to be there. Also, I have already set JButton properties in the constructor itself as you mentioned. So, what else do I need to change.
Note: The button drag is working fine, although it isn't smooth as I expected.The JFrame background color is initially red but as the JButton is added , it resets to default. Can you execute the code and see what I am referring to, Thanks.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
//setBackground(Color.red);
getContentPane().setBackground(Color.red);
Jack Weasels
Greenhorn

Joined: Jan 04, 2012
Posts: 22
ContentPane() solved it.Thanks.I was looking at repaint () all this while.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: MouseMotionListener usage for Jbutton