Hi all,

I'm new to Java (after years of Visual Basic and VBA). So right now I'm going through a very steep learning curve.

I'm trying to figure out (while I bang my head on the keyboard) how to do the following in SWING:

- Show mouse coordinates (x, y) in a SWING Label.
(also show coordinates if outside the SWING Frame).

Does anybody have some code for this? I've already got Sun's Mouse-Motion Listener code, but I want it to run on it on a Form, as well as showing the coordinates if the mouse is outside of the frame.

Many thanks!

Use java.awt.MouseInfo That gives you
MouseInfo.getPointerInfo().getLocation().x and

You can use a swing Timer ( http://java.sun.com/docs/books/tutorial/uiswing/misc/timer.html )to run a method at regular intervals.In that method use MouseInfo to get the coords, then put them in a String that you use to set the text of the JLabel.

Hey jorgelex008 here's I'm going to attach 2 codes. The first one is to know if the mouse entered the component or not.

I tried this before java doesn't have any methods to know the coordinates outside the component, if you want to accomplish this you can create a transparent window and then retrieve screen coordinates on mouse click. Here's the code

Mouse entered the JFrame object as follow
	this.addMouseListener(new MouseAdapter(){
			public void mouseEntered(MouseEvent e) {
	private void remFrame_mouseEntered(MouseEvent e)
		lblStatus.setText("Status: Active...");
		Window win = (Window)e.getComponent();		
										======<  <=======	
	Mouse exited the JFrame object as follow
	this.addMouseListener(new MouseAdapter(){
			public void mouseExited(MouseEvent e){
			} );
	private void remFrame_mouseExited(MouseEvent e)
		lblStatus.setText("Status: idle...");

The second code is to know the coordinates inside a JFrame object.

Here's the code

public class mouseTracker extends JFrame/* implements MouseListener, MouseMotionListener */{
	public mouseTracker() {

	private void stopBeepActionPerformed(ActionEvent e) {
		// TODO add your code here
			beeep = false;
			stopBeep.setLabel("start beep");
		}else {
			beeep = true;
			stopBeep.setLabel("stop beep");

	private void initComponents() {
		 looks = UIManager.getInstalledLookAndFeels();	  
			//Motif PLAF
			//setting window look and feel @ index 2
			UIManager.setLookAndFeel(looks[ 2 ].getClassName() );
			}catch(Exception e){
			System.err.println("Can't set look and feel : "+ e);
		addMouseMotionListener(new MouseMotionAdapter(){
			public void mouseMoved(MouseEvent e){
					xyLocations.setText("x="+e.getX()+","+" y="+e.getY());
					Toolkit tk = Toolkit.getDefaultToolkit();					
		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
		// Generated using JFormDesigner Open Source Project license - unknown
		coordinates = new JLabel();
		xyLocations = new JLabel();
		stopBeep = new JButton();

		//======== this ========
		setTitle("mouse tracker");
		Container contentPane = getContentPane();

		//---- coordinates ----
		coordinates.setText("Mouse coordinates @");
		coordinates.setBounds(10, 10, 105, 25);
		xyLocations.setBounds(115, 10, 85, 25);

		//---- stopBeep ----
		stopBeep.setText("stop beep");
		stopBeep.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
		stopBeep.setBounds(new Rectangle(new Point(60, 45), stopBeep.getPreferredSize()));

		contentPane.setPreferredSize(new Dimension(215, 110));
		setSize(215, 110);
		// JFormDesigner - End of component initialization  //GEN-END:initComponents

	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
	// Generated using JFormDesigner Open Source Project license - unknown
	private JLabel coordinates;
	private JLabel xyLocations;
	private JButton stopBeep;	
	private UIManager.LookAndFeelInfo looks[];
	private boolean beeep = true;
	// JFormDesigner - End of variables declaration  //GEN-END:variables
	public static void main(String args[]){
		new mouseTracker();		

Actually I did my best effort to write this for you, I hope this benefits you. Happy coding!!!!


java doesn't have any methods to know the coordinates outside the component

Have you looked at java.awt.MouseInfo ?

Thanks a lot james, actually I've not tried it before, but I will.
I think this class became a deprecated class, do you have any substitute for it??


I think this class became a deprecated class...

On the contrary, it was new in 1.5 Very much current.

Thanks I didn't know that, I'll try it!!!