How get click position on JPanel?

L

leonardo.mcarreiro

How can I execute an action (or function) when the user will click
into the JPanel ???
Any examples may help-me...

tanks
 
J

Joe Attardi

How can I execute an action (or function) when the user will click
into the JPanel ???
You need to make a class that implements MouseListener. There are a
bunch of different mouse events - I forget them all off the top of my
head - so see the Javadocs. But a MouseListener is what you want. Or if
a click is all you're interested in, extend MouseAdapter.

Hope this helps..

Joe
 
C

cyprian

You need to make a class that implements MouseListener. There are a
bunch of different mouse events - I forget them all off the top of my
head - so see the Javadocs. But a MouseListener is what you want. Or if
a click is all you're interested in, extend MouseAdapter.

Hope this helps..

Joe

The getLocationOnScreen() method of the MouseEvent class that returns
a Point object is what you need. It gives the x,y coordinates,
absolute coordinates, of mouse event. If you want a position relative
to the container itself, that is the JPanel, then you might be
thinking of getMousePosition() of the component class that returns a
point relative to the position of the component.
I believe you know how to extend the mouseadapter or implement the
various mouselisteners.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top