Redispatching MouseEvent to JMenuBar
I have a method to redispatch mouse events intercepted by a custom glass pane component. The redispatches work fine for everything except the JMenuBar I'm using in my JFrame. The JMenuBar just doesnt respond to the redispatched mouse events, and I don't understand why. I've looked around in the forums, but can't find any solutions, sadly.
'menuBar', 'contentPane' and 'glassPane' in the code below are what the names imply. They are all definitely correct references and not null at the time of execution.
Your suggestions and advice would be very much appreciated.
Here's the code I currently have:
privatevoid redispatchMouseEvent(MouseEvent event){
Point glassPanePoint = event.getPoint();
Container container = contentPane;
//
// Get mouse event point in content pane's coordinate system
//
Point containerPoint = SwingUtilities.convertPoint(glassPane, glassPanePoint, contentPane);
//
// If we're not in the content pane
//
if (containerPoint.y < 0){
//
// Then we're either in the menu bar..
//
if (containerPoint.y + menuBar.getHeight() >= 0){
//
// Get mouse event point in menu bar's coordinate
// system
//
Point menuBarPoint = SwingUtilities.convertPoint(glassPane, glassPanePoint, menuBar);
//
// Redispatch mouse event to the menu bar
//
menuBar.dispatchEvent(new MouseEvent(menuBar, event.getID(), event.getWhen(), event.getModifiers(), menuBarPoint.x,
menuBarPoint.y, event.getClickCount(), event.isPopupTrigger()));
//
// ..or else we're over a non-system window decoration
//
}else{
}
}else{
//
// Get deepest visible component lying under the mouse event
//
Component component = SwingUtilities.getDeepestComponentAt(container, containerPoint.x, containerPoint.y);
//
// Do nothing if no component is found
//
if (component !=null){
//
// Get mouse event point in deepest component's coordinate
// system
//
Point componentPoint = SwingUtilities.convertPoint(glassPane, glassPanePoint, component);
//
// Redispatch mouse event to the deepest component
//
component.dispatchEvent(new MouseEvent(component, event.getID(), event.getWhen(), event.getModifiers(), componentPoint.x,
componentPoint.y, event.getClickCount(), event.isPopupTrigger()));
}
}
}
# 1
> Your suggestions and advice would be very much appreciated.
Yet again we don't have a SSCCE to play with. Is this about the 5th time I've asked for a SSCCE?
Maybe we know the answer and maybe we don't.
Presumably you copied the code the the "How to Use Glass Panes" tutorial I pointed you to earlier. So if the code written by Sun doesn't work, then chances are we are not going to spot the problem simply by looking at the code.
So, if we don't know the answer but are interested in trying to solve the problem, why should we all waste time trying to create a simple demo that replicates the conditions you are trying to test?
How hard is it to create a simple JFrame with a menu bar and a few menus? If you do it once it takes 5 minutes. If 10 of us do it once it takes 50 minutes.
Most of the people here on the forum read and answer many questions daily. We don't have time to create a simple test program. So most people will read the question an move on. I guess thats what you want. When I looked at the question 25 people had previously viewed the question and moved on. Lets make that 26.
# 2
> Yet again we don't have a SSCCE to play with. Is this
> about the 5th time I've asked for a SSCCE?
Nope. It's gotta be WAY more than that ;) I'd guess 11 at least.
All I'm looking for is a simple 'yes' or 'no' to the question: Is what I've written correct?
I mean, you must have enough experience to tell me whether or not the above code should, in general, work. Presumably I am able to redispatch a mouse event for the JMenuBar in the same manner as any other component, yes? If so, then I know the problem lies elsewhere and I'll track it down eventually. If not, then perhaps you'd be kind enough to tell me what the redispatch code for a JMenuBar should be.
Redispatched events work perfectly for JButtons, JCheckBoxes and custom image panes I have in the interface. Which leads me to believe that there's some trick to redispatching a MouseEvent for a JMenuBar. After much searching, I still haven't established what that trick is.
# 6
> > If you have used the glass pane code in the Sun
> tutorial, you would
> > know that the MouseEvents aren't passed through to
> the JMenuBar in the code given
>
> As it turns out my version of the code was from the
> old tutorial, where they do pass events to the
> components on the menubar. Who know why they changed
> the demo. maybe the code doesn't work on JDK6.
>
> The difference would appear to be that you are
> dispatching the events to the menuBar, not the menu.
> Try using the getDeepestComponent() method like you
> are for the content pane.
Ah, I see. I'll give it a shot; thank you very much camickr, I'm grateful.