aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Printing mouse cordinates Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Printing mouse cordinates" Watch "Printing mouse cordinates" New topic
Author

Printing mouse cordinates

Nathan Papes
Greenhorn

Joined: May 05, 2012
Posts: 4
My goal is to print the mouse coordinates when the mouse is clicked within a frame and displayed in a text box. Here is my code so far, please help!

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19692
    
  20

Two things:
1) line 24 contains invalid syntax.
2) You are right to use a MouseListener, but you never start using it.

That said, your code has a few more problems. Some severe, like not implementing all of the methods of MouseListener and WindowsListener. My advice:
1) Drop AWT. Use Swing instead. AWT is very old and very limited. Swing can do just about anything AWT can do, plus more.
2) Use anonymous inner classes for your listeners. That way you can extend the adapter classes, and you don't need to implement all methods. For instance:
Your class then doesn't need to implement MouseListener or WindowListener anymore.

Speaking of the WindowListener, if you use a JFrame you can achieve the same behaviour with just one line:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Nathan Papes
Greenhorn

Joined: May 05, 2012
Posts: 4
I agree that AWT is dated, but for following my project guidelines it was asked to be used.

I need help with the mouselistener code

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19692
    
  20

And I've given you all the help you need. Just re-read my post carefully.
Nathan Papes
Greenhorn

Joined: May 05, 2012
Posts: 4
Rob Spoor wrote:And I've given you all the help you need. Just re-read my post carefully.


I have the code compiled and the program is functional, for that, thanks for all your help!

I have one more quick question(not a huge deal though), the program does not always return the coordinate values when the mouse is clicked??
I don't have much experience with java and troubleshooting, do you know an explanation for this?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> the program does not always return the coordinate values when the mouse is clicked??

mouseClicked() doesn't always fire, better to use mousePressed()
Nathan Papes
Greenhorn

Joined: May 05, 2012
Posts: 4
Michael Dunn wrote:> the program does not always return the coordinate values when the mouse is clicked??

mouseClicked() doesn't always fire, better to use mousePressed()


Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Printing mouse cordinates