MouseEntered and MouseExited in Swing Canvas

Hi there!



I am currently building a level editor for my game using a JMonkeyEngine Swing Canvas embedded in a NetBeans Platform application.

The program needs to react to the mouse entering and leaving the Canvas.

My first try was adding a mouseListener to the canvas or its parent Component and doing something in mouseEntered or mouseExited. Sadly this did not work; the methods were never called.



How can I detect whether the mouse is in the JMonkeyEngine Swing Canvas?



Thanks in advance!



jakobwenzel92

Yes, the jME3 canvas does not use the AWT input system. Have you tried registering a RawInputListener or using InputManager.getCursorPosition()?

A rawInputListener stopps generating MouseMotionEvents when I move the cursor outside the Canvas area, the InputManager’s getCursorPosition stays at the position seen in the last MouseMotionEvent. I do not see how this is helpful finding out if the mouse is inside the Canvas area.

I don’t think there’s such an option.

Consider using a jME AWT panel context which does support the AWT input system

Thanks, that one did work.

But now i get 10 FPS instead of 1000 FPS for an empty scene. Is AwtPanelsContext just slower or am i doing something wrong?



This is the code i use to initialize it:

[java]

public void addToComponent(final Container parent) {

final EditorApplication app = this;

SwingUtilities.invokeLater(new Runnable() {

public void run() {

final AwtPanelsContext ctx = (AwtPanelsContext) app.getContext();

panel = ctx.createPanel(PaintMode.Accelerated);



ctx.setInputSource(panel);

parent.add(panel);

}

});

}



private EditorApplication() {

setShowSettings(false);

AppSettings settings = new AppSettings(true);

settings.setCustomRenderer(AwtPanelsContext.class);

setSettings(settings);

setPauseOnLostFocus(false);

start();

}



public void simpleInitApp() {

super.simpleInitApp();



panel.attachTo(true, viewPort);

panel.attachTo(true, guiViewPort);



}

[/java]

I had to disable the canvas to get inputs reliable being received to the rest of the swing window (it seems once the canvas gets focus it never releases it). Were you able to solve that?



To work around your specific problem could you not reverse the logic? Detect the mouse entering/leaving the rest of your window and assume its over the renderer (or not on your app in which case you don’t care) the rest of the time…

Yeah, that is exactly the problem i have… Still no solution.



I thought about that one, but it is quite hard because in a NetBeans Platform Application the Opengl windows may be docked to another window from another Netbeans module which does not know anything about the Opengl Module and especially not about mouse events having to be redirected to it.