This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

MouseMotionListener usage for Jbutton

 
Jack Weasels
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


// could you explain me how to code the functionality for the Jbutton to move on mouse event: drag....thanks
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> Any suggestions ???

Any questions ???
 
Jack Weasels
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,
I modified some code , can you explain how I should go about it.
 
Stephan van Hulst
Bartender
Pie
Posts: 5414
52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 5414
52
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, issue resolved.
 
Jack Weasels
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//setBackground(Color.red);
getContentPane().setBackground(Color.red);
 
Jack Weasels
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ContentPane() solved it.Thanks.I was looking at repaint () all this while.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic