Hi Guys
I'm probably doing something stupid... but:
I've got a JFrame with borderlayout. In the centre box, I've got a
JPanel. If I add a mouselistener to the jframe, it works fine (but
I've got coords relative to the frame which is correct but not what I
want - I want coords relative to the panel). However, when I add the
mouselistener to the panel, I get no mouseevents being returned at all.
Any ideas in what way I might be being stupid?
TIA
Peter.
Knute Johnson - 04 Mar 2006 04:17 GMT
> Hi Guys
>
[quoted text clipped - 11 lines]
>
> Peter.
It is always easier for somebody to diagnose your problem if you include
your code. So all I can do is give you an example of how to add a
MouseListener to a JPanel. Don't forget that BorderLayout will cause
your component to expand to the size of your container. I usually only
use BorderLayout when I have one component to add to the container.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class test {
public static void createGUI() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel();
p.setBackground(Color.BLUE);
p.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
System.out.println(me.getX()+" "+me.getY());
}
});
f.add(p,BorderLayout.CENTER);
f.setSize(640,480);
f.setVisible(true);
}
public static void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
createGUI();
}
};
EventQueue.invokeLater(r);
}
}

Signature
Knute Johnson
email s/nospam/knute/