• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

MouseMotionListener usage for Jbutton

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


// could you explain me how to code the functionality for the Jbutton to move on mouse event: drag....thanks
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> Any suggestions ???

Any questions ???
 
Jack Weasels
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,
I modified some code , can you explain how I should go about it.
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 15510
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, issue resolved.
 
Jack Weasels
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
//setBackground(Color.red);
getContentPane().setBackground(Color.red);
 
Jack Weasels
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ContentPane() solved it.Thanks.I was looking at repaint () all this while.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic