java - How to implement MouseWheelListener for JPanel without breaking its default implementation? -


simply; have jpanel inside jscrollpane;

as expected; jscrollpane default listens mousewheelevent scrolling working when wheel rotating , while cursor hovering on jpanel.

but after that; updated jpanel implements mousewheellistener, , added mouse wheel listener jpanel itself.

@override public void mousewheelmoved(mousewheelevent e) {     if (e.iscontroldown()) {         if (e.getwheelrotation() < 0) {             system.out.println("mouse wheel up");         } else {             system.out.println("mouse wheel down");         }     } } 

the jpanel responds implementation when both; ctrl pressed down , wheel rotating , while cursor hovering on jpanel. default behaviour of jscrollpane unexpectedly lost!!!

when rotate wheel while cursor hovering on jpanel scrolls of jscrollpane not responding!!!

it seems that; implementation of mousewheellistener breaks default of jpanel.

so; how implement mousewheellistener jpanel without breaking default implementation?

mousewheelevents , scrolling behavior have subtle caveats.

for example, when open page (i mean one, reading), place mouse in middle, , start srolling down wheel, scroll on code snippets. note although code snippets contained in code blocks have scrollbar, continuously rotating mouse wheel not trigger scrolling in code blocks, in whole page. when once move mouse while inside code block, , afterwards roll mouse wheel, scroll in code block - , not whole page.

similarly, rotating mouse wheel may not affect hovered scrollable. think depends on window manager , & feel, in cases, scroll the scroll pane contains focussed component - if mouse cursor outside of component, , if on scollable component (you can observe this, example, in windows explorer)!


however, of these mechanisms , subtleties can found in swing components well. example, redispatching mechanism passes mousewheelevents ancestors if not handled component itself.

following pattern, solution (that conceptually similar the 1 luxxminer proposed, may tad more generic) may re-dispatch mousewheelevent parent component:

package stackoverflow;  import java.awt.dimension; import java.awt.gridlayout; import java.awt.event.mousewheelevent; import java.awt.event.mousewheellistener;  import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jscrollpane; import javax.swing.swingutilities;  public class mousewheellistenerforpanelinscrollpane {     public static void main(string[] args)     {         swingutilities.invokelater(new runnable()         {             @override             public void run()             {                 createandshowgui();             }         });     }      private static void createandshowgui()     {         jframe f = new jframe();         f.setdefaultcloseoperation(jframe.exit_on_close);          f.getcontentpane().setlayout(new gridlayout(1,2));          mousewheellistenerpanel m = new mousewheellistenerpanel();         m.setpreferredsize(new dimension(100,4000));         jscrollpane scrollpane = new jscrollpane(m);         f.getcontentpane().add(scrollpane);          f.setsize(500,500);         f.setlocationrelativeto(null);         f.setvisible(true);     } }  class mousewheellistenerpanel extends jpanel implements mousewheellistener {     mousewheellistenerpanel()     {         addmousewheellistener(this);     }      @override     public void mousewheelmoved(mousewheelevent e)     {         if (e.iscontroldown())         {             if (e.getwheelrotation() < 0)             {                 system.out.println("mouse wheel up");             }             else             {                 system.out.println("mouse wheel down");             }         }         else         {             getparent().dispatchevent(e);         }      } } 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -