954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to display location at mouse pointer?

I have posted one code.
please help me i can't get msg at mouse pointer.

please tell me how can i get label at the co-ordinates where the event has taken place.

also tell what is the problem with code..it gives output after minimizing and maximizing.

hope for reply.

Thank you.

/* ---------GTU Examination --- June-2011----Q.4(b)---solution-------*/

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;

public class mouse_evt extends Frame
{
	public String str = " ";
	public int mX = 0 , mY = 0;
	public Label l;
	
	public mouse_evt()
	{
		setTitle("My frame");
		setLayout(new FlowLayout());
		l = new Label(str + mX + mY );
		l.setLocation(mX,mY);
		add(l);
		addWindowListener(new myWindowAdapter());
		addMouseMotionListener(new mymouselistener(this));
		addMouseListener(new mymouse(this));
	}
	
	public static void main(String args[])
	{
		mouse_evt f = new mouse_evt();
		f.setSize(500,400);
		f.setLocation(450,270);
		f.setVisible(true);
		
	}
}

class myWindowAdapter extends WindowAdapter
{
	public void windowClosing(WindowEvent we)
	{
		System.exit(0);
	}
}
	
class mymouselistener extends MouseMotionAdapter
{
	mouse_evt mt;
	public mymouselistener(mouse_evt mt)
	{
		this.mt = mt;
	}
	public void mouseDragged(MouseEvent me)
	{
		mt.mX = me.getX();
		mt.mY = me.getY();
		mt.str = "Mouse Dragged at : ";
		mt.l.setText(mt.str + mt.mX + " " + mt.mY);
		mt.repaint();
	}
}	

class mymouse extends MouseAdapter
{
	mouse_evt mt;
	public mymouse(mouse_evt mt)
	{
		this.mt = mt;
	}
	
	public void mouseClicked(MouseEvent me)
	{
		mt.mX = me.getX();
		mt.mY = me.getY();
		mt.str = "Mouse clicked at:";
		mt.repaint();
		mt.l.setText(mt.str + mt.mX + "," + mt.mY);		
	}		
	public void mousePressed(MouseEvent me)
	{
		mt.repaint();
		mt.mX = me.getX();
		mt.mY = me.getY();
		mt.str = "Mouse pressed at : ";
		mt.l.setText(mt.str + mt.mX + "," + mt.mY);
	}
       	public void mouseReleased(MouseEvent me)
	{
		mt.repaint();
		mt.mX = me.getX();
		mt.mY = me.getY();
		mt.str = "Mouse Released at : ";
		//System.out.println(mt.str + mt.mX + mt.mY);
		mt.l.setText(mt.str + mt.mX + "," + mt.mY);
	}
}
jalpesh_007
Newbie Poster
11 posts since Sep 2010
Reputation Points: -2
Solved Threads: 1
 

mmm ... I don't really get what it is you're trying to do. if you want an action to perform on clicking a label, MouseClicked should be able to do that for you

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 
how can i get label at the co-ordinates where the event has taken place.


Look at the Container class's API doc.
It has some methods that could be useful.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

you have to add some JComponent to the JFrame, then add MouseXxxListener to this JComponent

mKorbel
Veteran Poster
1,141 posts since Feb 2011
Reputation Points: 480
Solved Threads: 224
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: