Click here to Skip to main content
13,398,920 members (32,026 online)
Click here to Skip to main content
Add your own
alternative version


22 bookmarked
Posted 9 Jun 2000

Add Mouse Wheel support to Swing Widgets

, 2 Jul 2000
Rate this:
Please Sign up or sign in to vote.
This article shows how to add support for Mouse Wheel for Java Swing Widgets
<!-- Link to source file download -->
  • Download source files - 14 Kb <!-- Add the rest of your HTML here -->


    There are at least five Request for Enhancement's (RFE) in the JavaSoft bug database related to Mouse Wheel support in Java. One of the RFE's BugID #4202656 has 281 votes from developers requesting Sun for a fix. Sun has finally agreed to support this feature in JDK 1.4 codenamed Merlin accroding to the BugID #4289845 in its bug database.

    This article is intended for those who don't want to wait till then or cannot move to a new JDK whenever Sun releases them. It shows how you can add support for Mouse Wheel with a little JNI code.

    Searching for a solution

    The first step was to develop a sample where this functionality can be tested. A sample from looked ideal with a few modifications. The screenshot of this sample is shown below. <!-- Article image -->

    Sample Image - mousewheel.gif

    The next step was to use Spy++ to find out which window was actually getting WM_MOUSEWHEEL message and see if the message can be trapped. Spy++ showed only one Heavy weight window as all the Swing controls are light-weight implementations that share the DC of the parent frame. Also the Messages window confirmed that mouse wheel messages on any child Swing control was sent to this parent frame window. See the screen show which shows the window hierarchy and the messages. <!-- Article image -->

    Sample Image - spy.gif

    Game Plan

    Given the information above, implementation was kind of straight forward. To enable mouse wheel scrolling in a Swing widget, the WM_MOUSEWHEEL messages have to intercepted in the parent frame and somehow posted to the widget. Here's a simple plan to do this.

    • Step 1: Get the HWND of the parent JFrameEx.
    • Step 2: Subclass the HWND using SetWindowLong(hwnd, GWL_WNDPROC,(LONG)FrameWindowProc);
    • Step 3: When FrameWindowProc is called with a WM_MOUSEWHEEL, notify the java JFrameEx object.
    • Step 4: In the JFrameEx's notifyMouseWheel function, figure out which scrollpane should receive the message and call the setValue of it's vertical scrollbar
    Step 1: This was easy using an FAQ on jguru. Here's the code snippet from
    // Returns the HWND for canvas.
    // This is undocumented, but works on JDK1.1.8, JDK1.2.2 and JDK1.3
    public int getHWND()
        DrawingSurfaceInfo drawingSurfaceInfo;
        Win32DrawingSurface win32DrawingSurface;
        int hwnd = 0;
        // Get the drawing surface
        drawingSurfaceInfo =
        if (null != drawingSurfaceInfo) {
            // Get the Win32 specific information
            win32DrawingSurface =
            hwnd = win32DrawingSurface.getHWnd();
        return hwnd;

    Step 2: Create a native entry point (using JNI) where we can call SetWindowLong. Here's the code snippet from MouseWheel.CPP.

    // JNI native entry point for subclassing the window
    JNIEXPORT void JNICALL Java_JFrameEx_setHook
      (JNIEnv *pEnv, jobject f, jint hwnd)
    	// ensure that the java object can be called from any thread.
    	jobject frame = pEnv->NewGlobalRef(f);
    	WNDPROC oldProc = (WNDPROC)::SetWindowLong((HWND)hwnd, GWL_WNDPROC, (LONG)FrameWindowProc);
    	// store the java object
    	setFrame((HWND)hwnd, frame);
    	// store the old window proc
    	setProc ((HWND)hwnd, oldProc);

    Step 3: Here's the code snippet from MouseWheel.CPP which deals with the WM_MOUSEWHEEL message.

    // Window Proc for subclassing.
    LRESULT CALLBACK FrameWindowProc(
      HWND hwnd,      // handle to window
      UINT uMsg,      // message identifier
      WPARAM wParam,  // first message parameter
      LPARAM lParam   // second message parameter
    	// Get the WindowProc for this window.
    	WNDPROC oldProc = getProc((HWND)hwnd);
    	// When we get a mouse wheel message
    	if(uMsg == WM_MOUSEWHEEL)
    		MSG *pMsg = new MSG;
    		pMsg->hwnd = hwnd;
    		pMsg->message = uMsg;
    		pMsg->wParam = wParam;
    		pMsg->lParam = lParam;
    		// send it to the java code from a new thread.
    		_beginthread(StartNotifyThread, 0, pMsg);
    		return 0;
    	return ::CallWindowProc(oldProc, hwnd, uMsg, wParam, lParam);
    // Helper Thread for notification.
    void WINAPIV StartNotifyThread(LPVOID lpVoid)
    	MSG *pMsg = (MSG *)lpVoid;
    	// This is the java object that needs to be notified.
    	jobject frame = getFrame(pMsg->hwnd);
    	// extract info from the wparam and lparam.
    	WPARAM fwKeys = LOWORD(pMsg->wParam);
    	short zDelta  = HIWORD(pMsg->wParam);
    	LPARAM xPos = GET_X_LPARAM(pMsg->lParam); 
    	LPARAM yPos = GET_Y_LPARAM(pMsg->lParam); 
    	// call the helper function.
    	delete pMsg;
    // This helper is the one that actually invokes the java JFrameEx's notifyMouseWheel method
    void notifyMouseWheel(jobject frame,short fwKeys,short zDelta,long xPos, long yPos)
    	HMODULE hMod	= NULL;
    	JavaVM *vmBuf	= NULL;
    	JNIEnv *pEnv	= NULL;
    	jsize bufLen	= 1;
    	jint nVMs		= 0;
    	// Get the Java VM.
    	pJNI_GetCreatedJavaVMs pFunc = getJavaVM();
    	jint nRet = (*pFunc)(&vmBuf,bufLen,&nVMs);
    	// Attach this thread.
    	vmBuf->AttachCurrentThread((void **)&pEnv,NULL);
    	// Inform the java object that the child has been created.
    	jclass cls = pEnv->GetObjectClass(frame);
    	jmethodID mid = pEnv->GetMethodID(cls, "notifyMouseWheel", "(SSJJ)V");
    	if (mid == 0)
    	pEnv->CallVoidMethod(frame, mid, (jshort)fwKeys, (jshort)zDelta, (jlong)xPos, (jlong)yPos);
    	// Detach this thread.

    Step 4: Here's the code snippet from which is called from the previous step. The only trick here is that we need to get the scrollpane of the widget. From the documentation of JScrollPane we see that we have to call widget.getParent().getParent() to get the widget's scrollpane.

    // this is the function which serves as a call back when 
    	// a mouse wheel movement is detected.
    	public void notifyMouseWheel(short fwKeys,short zDelta,long xPos, long yPos)
    		// Convert screen coordinates to component specific offsets.
    		Point p = new Point((int)xPos,(int)yPos);
    		// Find the embedded Swing component which should receive the scroll messages
    		Component c = SwingUtilities.getDeepestComponentAt(this, p.x, p.y);
    			// Get the scroll pane for the widget.
    			JScrollPane scrollPane = (JScrollPane) c.getParent().getParent();
    			// Get the vertical scrollbar for the scroll pane.
    			JScrollBar scrollBar = scrollPane.getVerticalScrollBar();
    			// Get the current value and set the new value depending on 
    			// the direction of the mouse wheel.
    			int nValue = scrollBar.getValue();
    			nValue = nValue + ((zDelta > 0) ? 5 : -5);
    		} catch (Exception e){
    			// Ignore exceptions.

    That's it folks.


    • There is a Build.bat in the zip file which can be use to build the sources. please edit this file to set the location of your VC++ and JDK 1.2.2 directories
    • To run the code use "java ScrollDemo2"
  • License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    About the Author

    Davanum Srinivas
    United States United States
    No Biography provided

    You may also be interested in...

    Comments and Discussions

    GeneralJava Coonnectivity with MSSql I got some given below correct and reply me plz Pin
    Maroof Khan9-Nov-10 10:41
    memberMaroof Khan9-Nov-10 10:41 
    GeneralDrawingSurfaceInfo error Pin
    nereidahoxhaj27-Feb-04 6:16
    membernereidahoxhaj27-Feb-04 6:16 
    GeneralRe: DrawingSurfaceInfo error Pin
    DeepakRamaswamy29-Mar-04 7:02
    memberDeepakRamaswamy29-Mar-04 7:02 
    GeneralRe: DrawingSurfaceInfo error Pin
    sabit4-May-04 5:46
    membersabit4-May-04 5:46 
    GeneralMouse Wheel (middle button) on Linux/Unix Pin
    Anup Engineer20-Feb-02 4:07
    memberAnup Engineer20-Feb-02 4:07 
    Generalcs1.keyboard lib. in java Pin
    vi25-Nov-01 8:15
    membervi25-Nov-01 8:15 
    GeneralRe: cs1.keyboard lib. in java Pin
    Anonymous7-Jun-04 13:22
    sussAnonymous7-Jun-04 13:22 
    GeneralRe: cs1.keyboard lib. in java Pin
    Anonymous19-Oct-05 14:31
    sussAnonymous19-Oct-05 14:31 
    Generaljava swing Pin
    admie17-Aug-01 4:46
    memberadmie17-Aug-01 4:46 
    GeneralRe: java swing Pin
    Anonymous8-Nov-01 13:25
    memberAnonymous8-Nov-01 13:25 
    GeneralRe: java swing Pin
    Anonymous3-Apr-02 4:36
    memberAnonymous3-Apr-02 4:36 
    GeneralCode Maintained on SourceForge Pin
    Anonymous10-Aug-01 2:07
    memberAnonymous10-Aug-01 2:07 
    GeneralGL problems Pin
    soqw9-Jan-01 0:27
    membersoqw9-Jan-01 0:27 
    GeneralLinux & Wheel Mouse Pin
    Anonymous16-Dec-00 6:37
    memberAnonymous16-Dec-00 6:37 
    GeneralJava Applets & Mouse Wheel Pin
    bike14-Jun-00 8:55
    sussbike14-Jun-00 8:55 
    GeneralRe: Java Applets & Mouse Wheel Pin
    Anonymous13-Jul-01 6:50
    memberAnonymous13-Jul-01 6:50 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    Permalink | Advertise | Privacy | Terms of Use | Mobile
    Web01 | 2.8.180218.2 | Last Updated 3 Jul 2000
    Article Copyright 2000 by Davanum Srinivas
    Everything else Copyright © CodeProject, 1999-2018
    Layout: fixed | fluid