Why is creating a mouse listener so damn difficult?

K.I.L.E.R

Retarded moron
Veteran
C:\Documents and Settings\Kruno\Desktop\event\eve.java:14: addMouseListener(java.awt.event.MouseListener) in java.awt.Component cannot be applied to (myprojects.event.eve)
btn.addMouseListener(this);

Code:
package myprojects.event;

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

public class eve extends JFrame
{	
	public eve()
	{
		JTextArea txt = new JTextArea(10,10);
		JButton btn = new JButton("sadsad");
		btn.addMouseListener(this);
		JList lst = new JList();
		
		/////////////
		String listed[] = new String[10];
		
		for(int i=0; i < 10; i++)		
			listed[i] = 11111*1;
			
		/////////////
		lst.setListData(listed);
		
		//////////////
		txt.setBorder(BorderFactory.createBevelBorder(1));
		lst.setBorder(BorderFactory.createEtchedBorder(1));
		//////////////	
		
		///////////////
		Container cont = getContentPane();
		JPanel pane = new JPanel();
		
		//pane.setLayout(new GridLayout(0,1));
		pane.add(btn);
		pane.add(txt);
		pane.add(lst);
		cont.add(pane);
	}
	
  public void mouseClicked(MouseEvent event)
  {
  }
	
	public static void main(String args[]) 
	{		
		eve frame = new eve();
		
		frame.setTitle("u suck");
		
		frame.setVisible(true);
		frame.validate();
		frame.pack();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
}
 
"Why is creating a mouse listener so damn difficult?"

When I saw that I wondered if it was somehow related to a horse whisperer.
 
Simon F said:
"Why is creating a mouse listener so damn difficult?"

When I saw that I wondered if it was somehow related to a horse whisperer.

This got me laughing for a good few minutes. :LOL:
 
Thank You PiNKY, works perfect. There is one little annoying thing, is there a way to avoid having to overwrite all the abstract methods?
 
Yes...

You want to use the Adapter design-pattern. You might want to go on the net and read a little bit about Adapters, and why they're very useful in OO languages.

In essence, the Adapter will allow you to utilize the functionality of the Listener that you're interested in. In other words, if you only care about one specific function within the interface, the Adapter provides a mechanism to deal with that 1 and only function.
 
Point to note though: There is no multiple inheritance in Java so if you want a particular instance of EVE acting as a Listener you cannot use Adapters. You could however just define a member class like btn.addMouseListener(new MouseListenerAdapter <Don't know the exact name..but there are a bunch of predifined Listener adapters available in Java>() { <define your own Mouseclicked here>}) and still retaining access to Eve's members...
 
I have figured that out yesterday Pinky. Thanks though. :)

button.addMouseListener
(
new MouseListener()
{
public void mouseClicked(MouseEvent e)
{
}
}
);

I tried that before but it didn't work, I found out why. I didn't import:

import javax.swing.event.MouseInputAdapter;

I prefer to use:

import javax.swing.event.*;

In case I want any future event adapters or the like.

BTW: When is there going to be multiple inheritence in Java? Java 1.5?
 
It's more an issue of philosophy than anything else. There have been situations in which I sorta' wish .NET allowed multiple inheritance, but at the same time, I understand why this is also considered "bad."

Go to google and do a search on the subject, and you will turn up many hits that discuss this subject.
 
The way most coders get around the multiple-inheritence is through the use of interfaces and utilizing the delegate pattern (proxying / uml "uses" notation). The coding for this can be a bit tedious at times, which is why I created several code-generator plugins for the typical modeling tools (Rational Rose and Together ControlCenter).

The fundamental issue with multiple-inheritence is the 'diamond issue'. It's talked a bit about here: http://www.javaworld.com/javaworld/jw-12-1998/jw-12-techniques.html.

The reason I prefer not to see multiple-inheritence is I experience enough abuse and incompetence in regular Java code that I took over from others without the added complexity of multiple-inheritence. If they can't grasp simpler concepts, I'd hate to see them with more complex notions.

example of delegate pattern where class C has multiple inheritence from class A, B, and C while providing unique behavior when needed:
Code:
public class ABC implements AInterface, BInterface, CInterface
{
    private A myA = new A();
    private B myB = new B();
    private C myC = new C();

    public boolean methodFromBInterface()
    {
         return B.methodFromBInterface();
    }

    public boolean methodFromAInterface()
    {
        return A.methodFromAInterface();
    }

    public void methodFromCInterface()
    {
        // do own work for this method ...
    }
}
 
Back
Top