I came up with a very hacky solution.
I made a UI class that extends BasicMenuUI. I override the createMouseInputListener
method to return a custom MouseInputListener
instead of the private handler
object inside BasicMenuUI
.
I then got the code for the MouseInputListener
implementation in handler
from GrepCode[1], and copied it into my custom listener. I made one change, putting a timer in mouseEntered
. My final code for mouseEntered
looks like this:
public void mouseEntered(MouseEvent e) {
timer.schedule(new TimerTask() {
@Override
public void run() {
if (menuItem.isShowing())
{
Point mouseLoc = MouseInfo.getPointerInfo().getLocation();
Point menuLoc = menuItem.getLocationOnScreen();
if (mouseLoc.x >= menuLoc.x && mouseLoc.x <= menuLoc.x + menuItem.getWidth() &&
mouseLoc.y >= menuLoc.y && mouseLoc.y <= menuLoc.y + menuItem.getHeight())
{
originalMouseEnteredStuff();
}
}
}
}, 100);
}
Before calling the the original code that was in mouseEntered
, I check to make sure the mouse is still within this menu's area. I don't want all the menus my mouse brushes over to pop up after 100 ms.
Please let me know if anyone has discovered a better solution for this.