• 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

Printing mouse cordinates

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!

 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
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
> 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
I am not a spy. Definitely. Definitely not a spy. Not me. No way. But this tiny ad ...
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic