override addMouseListener?

Hi,

I have a JPanel with a number of child controls. This JPanel is intended for use in a larger JFrame which has several other components, such as JFileChooser. I want the JFrame to know in which component the user is clicking.

If I add mouse listeners for the JFileChooser, it reports all clicks to the JFrame perfectly. However, doing the same for my JPanel only reports clicks for the JPanel itself, no clicks on the JPanel's child components report clicks to the JFrame. (i.e. if there is a button in the JPanel, clicks on that button are not reported to the JFrame. Only clicks to the grey spaces in the JPanel are reported)

This is the code I use in the JFrame:

myJPanel.addMouseListener(new MouseListener(){

publicvoid mouseClicked(MouseEvent evt){

//do something with click info

}

I've tried overriding the JPanel's addMouseListener, but it doesn't work. If I use the code:

@Override

publicvoid addMouseListener(MouseListener l){

super.addMouseListener(l);

}

It works exactly as before (obviously). But If I try adding any useful code, such as below, then all listener functions are lost - no clicks to the JPanel or its child components are reported)

@Override

publicvoid addMouseListener(MouseListener l){

super.addMouseListener(l);

MouseListener[] ml = jButton_in_JPanel.getMouseListeners();

for(int i = 0; i < ml.length; i++){

super.addMouseListener(ml[i]);

}

}

What am I doing wrong?

[2112 byte] By [seabhcana] at [2007-11-27 7:31:35]
# 1
My two cents: You could add a mouse listener for each Component of your JPanel.
java_knighta at 2007-7-12 19:11:50 > top of Java-index,Desktop,Core GUI APIs...
# 2
but then how does the JFrame access those events?
seabhcana at 2007-7-12 19:11:50 > top of Java-index,Desktop,Core GUI APIs...
# 3

> but then how does the JFrame access those events?

You can implement your JFrame as a MouseListener, then

you can register it into your other panels. Something like:

Implementing MouseListener

public class MyFrame extends JFrame implements MouseListener {}

Registering

JPanel myPanel = new JPanel();

myPanel.addMouseListener(myFrame);

Edit: Note that this is not the way I would do it. Usually I

like to put my listeners in different classes. I'm just

answering what you asked for, your JFrame listening to

mouse events of child panels.

Message was edited by:

Danniel_Willian

Danniel_Williana at 2007-7-12 19:11:50 > top of Java-index,Desktop,Core GUI APIs...
# 4
Thanks.But thats a bit messy. How does JFileChooser do it? If I listen for clicks in my JFrame all clicks are returned, even those to JFileChooser child buttons. I want to be able to get that functionality into my JPanel.
seabhcana at 2007-7-12 19:11:50 > top of Java-index,Desktop,Core GUI APIs...
# 5

You can use an AWTEVentListener to listen for all events generated:

Toolkit.getDefaultToolkit().addAWTEventListener( new AWTEventListener()

{

public void eventDispatched(AWTEvent e)

{

System.out.println(e);

}

}, AWTEvent.MOUSE_MOTION_EVENT_MASK + AWTEvent.MOUSE_EVENT_MASK);

camickra at 2007-7-12 19:11:50 > top of Java-index,Desktop,Core GUI APIs...
# 6
Solved it.I can just add the statement "this.dispatchEvent( evt );" to each child control event code statement. Works a treat.
seabhcana at 2007-7-12 19:11:50 > top of Java-index,Desktop,Core GUI APIs...