odd mouselistener issue

Discussion in 'Java' started by Peter Ashford, Mar 4, 2006.

  1. 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?


    Peter Ashford, Mar 4, 2006
    1. Advertisements

  2. 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();
    JPanel p = new JPanel();
    p.addMouseListener(new MouseAdapter() {
    public void mousePressed(MouseEvent me) {
    System.out.println(me.getX()+" "+me.getY());

    public static void main(String[] args) {
    Runnable r = new Runnable() {
    public void run() {
    Knute Johnson, Mar 4, 2006
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.