java - Prevent mouseExited from being called when exiting into a Popup -
i'm trying implement own tooltip. timer
, mouse listeners (for moved , exited). when mouse moved, timer reset, popup shows when mouse has been still. when mouse exits component, popup hidden. however, when popup shown @ cursor, mouse inside popup, mouseexited
called. popup disappears, but, if mouse remains still, happens again, causing popup flicker. can prevented moving popup 1px over, mouse isn't in popup, doesn't solve whole problem, because moving mouse on popup makes disappear.
my mcve:
private static timer timer = new timer(100, new actionlistener() { @override public void actionperformed(actionevent e) { jpanel pop = new jpanel(new gridlayout(0, 3)); pop.setbackground(color.blue); // calculations similar happening, // because otherwise flicker fast demonstrate on screen (double = math.random() * 12; < 40; i++) { bufferedimage img = new bufferedimage(32, 32, bufferedimage.type_int_argb_pre); graphics2d g = img.creategraphics(); g.drawstring(math.log(math.sqrt(math.random())) + "", 0, 32); g.dispose(); pop.add(new jlabel(new imageicon(img))); } popup = popupfactory.getsharedinstance().getpopup(panel, pop, x, y); popup.show(); } }); private static jpanel panel; private static popup popup; private static int x, y; public static void main(string[] args) { jframe frame = new jframe(); frame.setsize(640, 480); frame.setdefaultcloseoperation(jframe.exit_on_close); mouseadapter ma = new mouseadapter() { @override public void mousemoved(mouseevent e) { timer.setrepeats(false); if (popup == null) timer.restart(); x = e.getxonscreen(); // adding 1 here eliminates flicker problem, y = e.getyonscreen(); // still calls mouseexited entering popup } @override public void mouseexited(mouseevent e) { if (popup != null) { popup.hide(); popup = null; } timer.stop(); } }; panel = new jpanel(); panel.setbackground(color.green); panel.addmouselistener(ma); panel.addmousemotionlistener(ma); frame.add(panel); frame.setvisible(true); }
i thinking maybe mouseexited
method should check see if popup's mouseentered
called, i'm not sure how this, , cause problems when popup extends on edges of component. prefer mouse listener ignore popup.
it ok if popup moved mouse, i'm not sure how either.
however, when popup shown @ cursor, mouse inside popup, mouseexited called.
you can check mouseexited event coordinates occurred within bounds of jpanel
, , hide popup
if event occurred outside jpanel
bounds.
if ( panel.contains(e.getx(), e.gety() ) ){ return; }
consider using tool tips - event handling , highly customizable...multi-line achievable via html
, can change color changing , feel
Comments
Post a Comment