Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: MouseListener

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    20
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default MouseListener

    import java.awt.Graphics;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
     
    import javax.swing.JFrame;
     
     
     
    public class MouseEvent implements MouseListener,MouseMotionListener
    {
     
    	static JFrame f = new JFrame();
    	String msg = " ";
    	int mouseX = 0,mouseY = 0;
     
    	public void init()
    	{
    		f.addMouseListener(this);
    		f.addMouseMotionListener(this);
     
    	}
     
    	@Override
    	public void mouseDragged(java.awt.event.MouseEvent arg0) {
    		// TODO Auto-generated method stub
     
    	}
     
    	@Override
    	public void mouseMoved(java.awt.event.MouseEvent arg0) {
    		// TODO Auto-generated method stub
     
    	}
     
    	@Override
    	public void mouseClicked(java.awt.event.MouseEvent e) {
    		// TODO Auto-generated method stub
    		mouseX = 0;
    		mouseY = 10;
    		msg = "Mouse clicked";
    		f.repaint();
    		System.out.println("Mouse clicked");
     
    	}
     
    	@Override
    	public void mouseEntered(java.awt.event.MouseEvent e) {
    		// TODO Auto-generated method stub
     
    	}
     
    	@Override
    	public void mouseExited(java.awt.event.MouseEvent e) {
    		// TODO Auto-generated method stub
     
    	}
     
    	@Override
    	public void mousePressed(java.awt.event.MouseEvent e) {
    		// TODO Auto-generated method stub
     
    	}
     
    	@Override
    	public void mouseReleased(java.awt.event.MouseEvent e) {
    		// TODO Auto-generated method stub
     
    	}
     
    	public void repaint(Graphics g)
    	{
    		g.drawString(msg,mouseX,mouseY);
     
    	}
     
    	   public static void main(String[] args) {
     
    		   	MouseEvent me = new MouseEvent();
    		   	me.init();
    		   	f.pack();
    	    	f.setVisible(true);
    	   }
     
     
     
    }
    The message is not shown on the frame.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: MouseListener

    Can you explain what you expect the posted code to do? For example what code is supposed to call the repaint() method defined in your MouseEvent class?

    BTW Using the class name of a Java SE class (MouseEvent) is a bad idea. Your class names should be unique.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    20
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: MouseListener

    The code,which i think,should print a message on the frame that the "Mouse is clicked",when the mouse is clicked. The repaint() will get called when the mouse is clicked.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: MouseListener

    The repaint() will get called when the mouse is clicked.
    There are two different repaint() methods. One without arguments and one with. The one without args belongs to the Component class. It is called in the mouseClicked() method. The posted code has the other repaint() method with the argument. The one with the argument is not called anywhere that I can see in the posted code. If it is not called, the drawString method will not be called.

    Your repaint() method is not in a component that is being displayed on any GUI like a JFrame. You need to look at how GUI coding works.
    Perhaps the tutorials:
    http://docs.oracle.com/javase/tutori...ing/index.html
    Last edited by Norm; June 23rd, 2012 at 09:31 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jun 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: MouseListener

    what is @overide? explain?

  6. #6
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: MouseListener

    It's @Override: an annotation. See Annotations in Oracle's Tutorial.
    Last edited by pbrockway2; June 30th, 2012 at 02:23 AM.

Similar Threads

  1. MVC with MouseListener
    By horvath62 in forum Java Theory & Questions
    Replies: 2
    Last Post: June 3rd, 2012, 09:26 AM
  2. Question about MouseListener
    By piulitza in forum AWT / Java Swing
    Replies: 1
    Last Post: April 18th, 2012, 08:30 AM
  3. MouseListener Conundrum
    By Dirnol in forum Java Theory & Questions
    Replies: 6
    Last Post: October 11th, 2011, 07:11 AM
  4. problem with the MouseListener
    By boaz1001 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: August 23rd, 2011, 07:23 AM
  5. MouseListener
    By _lithium_ in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 18th, 2011, 11:43 AM