odd mouselistener issue

P

Peter Ashford

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.
 
K

Knute Johnson

Peter said:
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.

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);
}
}
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top