Watch this demo, it shows how to transmit click events from the glass panel (where you draw additional content) to the underlying "real" panel (where you want clicks / events to happen). See Method redispatchMouseEvent. The following are some relevant parts of the code:
glass = new FixedGlassPane(jFrame.getContentPane());
jFrame.setGlassPane(glass);
public class FixedGlassPane extends JPanel
...
addMouseListener(this);
addMouseMotionListener(this);
addFocusListener(this);
...
}
public void mouseDragged(MouseEvent e) {
if (needToRedispatch)
redispatchMouseEvent(e);
}
public void mouseMoved(MouseEvent e) {
if (needToRedispatch)
redispatchMouseEvent(e);
}
... other mouse event methods ...
private void redispatchMouseEvent(MouseEvent e) {
boolean inButton = false;
boolean inMenuBar = false;
Point glassPanePoint = e.getPoint();
Component component = null;
Container container = contentPane;
Point containerPoint = SwingUtilities.convertPoint(this,
glassPanePoint, contentPane);
int eventID = e.getID();
if (containerPoint.y < 0) {
inMenuBar = true;
container = menuBar;
containerPoint = SwingUtilities.convertPoint(this, glassPanePoint,
menuBar);
testForDrag(eventID);
}
component = SwingUtilities.getDeepestComponentAt(container,
containerPoint.x, containerPoint.y);
if (component == null) {
return;
} else {
inButton = true;
testForDrag(eventID);
}
if (inMenuBar || inButton || inDrag) {
Point componentPoint = SwingUtilities.convertPoint(this,
glassPanePoint, component);
component.dispatchEvent(new MouseEvent(component, eventID, e
.getWhen(), e.getModifiers(), componentPoint.x,
componentPoint.y, e.getClickCount(), e.isPopupTrigger()));
}
}
private void testForDrag(int eventID) {
if (eventID == MouseEvent.MOUSE_PRESSED) {
inDrag = true;
}
}
}
source
share