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!

Hi,
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) {
				 remFrame_mouseEntered(e);
			}
			
			});
	private void remFrame_mouseEntered(MouseEvent e)
	{
		lblStatus.setText("Status: Active...");
		super.setLocation(310,0);
		super.setSize(390,300);
		Window win = (Window)e.getComponent();		
		win.requestFocus();			
		win.toFront();	
		win.setFocusable(true);
		jEditorPane.requestFocusInWindow();
	}
										======<  <=======	
	Mouse exited the JFrame object as follow
	
	this.addMouseListener(new MouseAdapter(){
			
			public void mouseExited(MouseEvent e){
				remFrame_mouseExited(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() {
		initComponents();
	}
	

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


	private void initComponents() {
		
		 looks = UIManager.getInstalledLookAndFeels();	  
		try{
			//Motif PLAF
			//setting window look and feel @ index 2
			UIManager.setLookAndFeel(looks[ 2 ].getClassName() );
			SwingUtilities.updateComponentTreeUI(this);
			}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();					
					if(beeep)
					  tk.beep();
			}
		});
		// 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");
		setFocusTraversalPolicyProvider(true);
		setResizable(false);
		setVisible(true);
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		Container contentPane = getContentPane();
		contentPane.setLayout(null);

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

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

		contentPane.setPreferredSize(new Dimension(215, 110));
		setSize(215, 110);
		setLocationRelativeTo(getOwner());
		// 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!!!!

[EL-Prince]

@JamesCherrill
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??

[EL-Prince]

@JamesCherrill
I think this class became a deprecated class...
[EL-Prince]

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

This question has already been answered. Start a new discussion instead.