Hi all,
I have two components, one on top of the other. Both have mouse listeners, and I'd like them both to respond to the mouse event, but the bottom component isn't being notified. How can I get the bottom component to be notified in the example below?
It would be great if there were some simple "setPassThrough(true)"-like method, but there doesn't seem to be anything like that. If I have to manually pass the mouse event to the bottom component, I can do that, but it seems complicated, because the MouseEvent is already in the co-ordinate system of the top component. If the two components were not aligned (they are aligned below, but it's a simplification), passing the MouseEvent to the bottom component gets all screwy.
What's the cleanest way of getting both components to respond to the mouse event?
Thanks!
Self-contained example below,
Tim
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingTester
{
private static JComponent createTesterPanel()
{
JPanel bottomPanel = new JPanel();
bottomPanel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e)
{
System.out.println("Bottom panel clicked!");
}
});
JPanel topPanel = new JPanel();
topPanel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e)
{
System.out.println("Top panel clicked!");
}
});
JPanel wrapper = new JPanel();
wrapper.setLayout(new OverlayLayout(wrapper));
wrapper.add(topPanel);
wrapper.add(bottomPanel);
return wrapper;
}
public static void main(String[] args)
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(createTesterPanel());
f.setPreferredSize(new Dimension(100, 100));
f.pack();
f.setVisible(true);
}
}