• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Printing mouse cordinates

 
Nathan Papes
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20559
57
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Nathan Papes
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20559
57
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And I've given you all the help you need. Just re-read my post carefully.
 
Nathan Papes
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic