5 Replies - 653 Views - Last Post: 16 November 2009 - 08:07 PM Rate Topic: -----

#1 putha-nee  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 168
  • Joined: 04-October 09

Probably a simple MouseEvent problem

Posted 16 November 2009 - 07:27 PM

I am trying to write a MouseEvent sample program and for some reason keep getting a compiling erro:

Tester$1> is not abstract and does not override abstract method mouseEntered(java.awt.event.MouseEvent) in java.awt.event.MouseListener

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;

public class Tester extends JFrame
{
 public static void main(String[] args)
 {
  JFrame frame = new JFrame();
  Container cp = frame.getContentPane();
  
  JPanel p1 = new JPanel();
  final JTextField t1 = new JTextField();
  final JButton button = new JButton();
  button.setText("Test1");
  button.addMouseListener(new MouseListener()
							{
	public void mouseExited(MouseEvent e)
	{
	  button.setText("Test2");
	}
  });
  p1.add(button);
  cp.add(p1);
  
  frame.setSize(600,400);
  frame.setVisible(true);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
 }
 
   public void mousePressed(MouseEvent e)
	{
	eventoutput("Mouse LEvent");
	}
}

This post has been edited by putha-nee: 16 November 2009 - 07:28 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Probably a simple MouseEvent problem

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,023
  • Joined: 15-July 08

Re: Probably a simple MouseEvent problem

Posted 16 November 2009 - 07:33 PM

Alternatively, instead of using a new MouseListener, you could use a MouseAdapter object. That object implements all of teh MouseEvents as blank ({}), but this allows you to only need to override the necessary one like so:

button.addMouseListener(new MouseAdapter() {
	public void mouseExited(MouseEvent e)
	{
	  button.setText("Test2");
	}
  });



Also, you do not need this at the end because you implemented it with the MouseAdapter:
   public void mousePressed(MouseEvent e)
	{
	eventoutput("Mouse LEvent");
	}



Just get rid of it...it serves no purpose.

This post has been edited by Dogstopper: 16 November 2009 - 07:48 PM

Was This Post Helpful? 0
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Probably a simple MouseEvent problem

Posted 16 November 2009 - 07:35 PM

The error message is telling you exactly what the problem is. Your mouse listener needs to account for each function. JFrame has a method called mouseEnter(also that particular one is deprecated unless you're still rocking the 1.1 JDK).
Was This Post Helpful? 0
  • +
  • -

#4 putha-nee  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 168
  • Joined: 04-October 09

Re: Probably a simple MouseEvent problem

Posted 16 November 2009 - 07:58 PM

So in the end, I realized you need to define all the mousePressed, mouseClicked methods, etc...even if they are empty just to ensure the compiler compiles the program. I don't know why but this is the only thing that worked for me.

Anyone care to state why this is?
Was This Post Helpful? 0
  • +
  • -

#5 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Probably a simple MouseEvent problem

Posted 16 November 2009 - 08:02 PM

You're inheriting an interface. Interface's methods are not defined, they are simply there for the inheriting class to override.


//syntax might be a bit off
public interface Animal{
	public void speak(); //if you try to define this, you'll get an error
}

public class Cat implements Animal{
  //the cat has to speak, must define it's own

 //the cat meows
  public void speak() {System.out.println("Meow!");
}

//whereas the dog barks
public class Dog implements Animal{
  public void speak() {System.out.println("Bark!");
}


Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,023
  • Joined: 15-July 08

Re: Probably a simple MouseEvent problem

Posted 16 November 2009 - 08:07 PM

Unless of course, you use an adapter class like i demonstrate in my above post and here:
http://java.sun.com/...useAdapter.html
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1