aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Writing Mouse Events Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Writing Mouse Events" Watch "Writing Mouse Events" New topic
Author

Writing Mouse Events

George Jacobs
Greenhorn

Joined: Nov 29, 2011
Posts: 5
Since my previous post has gotten much action I figured I would ask a more broad question.

How do I create a GUI that can have 2 mouse events on different "panes" and keep track of these? Is this doable?

For example, I have a JFrame with a mouse event and a JPanel with mouse events. Do I need to make one a keyboard event and keep the other as a mouse event?

Thanks!
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

no, that is not a problem just set them both to have the same mouseListener. then in your mouse handling code determine where the event came from. e.getsource(). or let the JFrame handle its and let the JPanel handle its. that might be the best way.


SCJP
Visit my download page
George Jacobs
Greenhorn

Joined: Nov 29, 2011
Posts: 5
Randall Twede wrote:no, that is not a problem just set them both to have the same mouseListener. then in your mouse handling code determine where the event came from. e.getsource(). or let the JFrame handle its and let the JPanel handle its. that might be the best way.


Thanks Randall!

I have created a class to attempt to try this, but I am having trouble getting a box to draw.

Here is my code associated with what I have done.



I had to implement all abstract methods, but the goal is to draw a rectangle with the mouselistener in the jpanel. I open the program and I get nothing besides the "Draw a box by dragging the mouse" message. What am I doing wrong?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
there's a bunch of things you have wrong, using old depecated methods, using paint() etc

here's a framework for you to play around with.

run it, click anywhere in the panel, drag the mouse sideways, then release it and check the console messages.

George Jacobs
Greenhorn

Joined: Nov 29, 2011
Posts: 5
Michael Dunn wrote:there's a bunch of things you have wrong, using old depecated methods, using paint() etc

here's a framework for you to play around with.

run it, click anywhere in the panel, drag the mouse sideways, then release it and check the console messages.



Michael, thank you so much for the help. I got the rectangle to draw, but I am not sure if it is the best way because of the repaint method. Are there any problems with repaint

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

repaint is the way you do it. never call paintComponent() directly. here i have some code that will help you.

there are 3 other classes in this program but the code i posted should help.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2203
    
    7


Don't use "==".

Use the equals() method.
George Jacobs
Greenhorn

Joined: Nov 29, 2011
Posts: 5
Thanks for the code. Haven't tried to implement yet, but will try soon.

My next question is how do I now calculate the coordinates of the rectangle after it is drawn. For example, I would like to know center x, y coordinates of rectangle, the four corner coordinates, and the width/height which could be derived by these listed.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

rob is correct the equals method is better.
 
wood burning stoves
 
subject: Writing Mouse Events