Click here to Skip to main content
Click here to Skip to main content

Embed ActiveX controls inside Java GUI

, 8 May 2000
Rate this:
Please Sign up or sign in to vote.
With this your Java projects can take advantage of ActiveX controls and Office documents such as spreadsheets, charts, calendars, word processors, specialized graphics, and many more.
<!-- Link to source file download -->
  • Download source and binary files - 6.9 Kb
  • <!-- Article image -->

    Sample Image - javacom.gif

    <!-- Add the rest of your HTML here -->

    Introduction

    This article provides a skeleton for embedding ActiveX controls within your Java Applications. The sample project shows how to embed the Internet Explorer 5.0 just like any other Java Widget.

    There are two major problems that needed to be solved to be able to do this.

  • Problem #1 - Get the HWND of a Java Heavy Weight component.
  • Problem #2 - How to attach an activex control as a child of the HWND above.
  • Problem #1 was solved by an undocumented API present in all versions of JDK's from Sun, for example JDK 1.1.8, JDK 1.2.2 and JDK 1.3. Here's the URL which explains how to get the HWND of a java.awt.Canvas object and an extract from the project which shows the implementation.

    http://www.jguru.com/jguru/faq/view.jsp?EID=44507

    // Returns the HWND for panel. This is a hack which works with
    // JDK1.1.8, JDK1.2.2 and JDK1.3. This is undocumented. Also 
    // you should call this only after addNotify has been called.
    public int getHWND() 
    {
        int hwnd = 0;
        DrawingSurfaceInfo drawingSurfaceInfo = ((DrawingSurface)(getPeer())).getDrawingSurfaceInfo();
        if (null != drawingSurfaceInfo) 
        {
            drawingSurfaceInfo.lock();
            Win32DrawingSurface win32DrawingSurface = (Win32DrawingSurface)drawingSurfaceInfo.getSurface();
            hwnd = win32DrawingSurface.getHWnd();
            drawingSurfaceInfo.unlock();
        }
        return hwnd;
    }
    

    Problem #2 was a bit more complicated but MSDN's Online KB was of great help in solving this. Here's the URL which explains how to add an ActiveX control as a child of any HWND using ATL and an extract from the project which shows how the concept is implemented.

    http://support.microsoft.com/support/kb/articles/Q192/5/60.ASP

    // Creates IE control
    VOID CreateIEControl(ThreadParam *pThreadParam)
    {
        AtlAxWinInit();
        printf("Create AtlAxWin Begin...[0x%x][%s]\n",pThreadParam->hwnd,pThreadParam->szURL);
        // In the 2nd Param you can use ProgID or UUID of any activex control.
        HWND hwndChild = ::CreateWindow("AtlAxWin",
                                        "Shell.Explorer.1", 
                                        WS_CHILD|WS_VISIBLE,
                                        0,0,0,0,
                                        pThreadParam->hwnd,NULL,
                                        ::GetModuleHandle(NULL),
                                        NULL);
    
        IUnknown *pUnk = NULL;
        AtlAxGetControl(hwndChild,&pUnk);
        printf("Create AtlAxWin Done...[0x%x]\n",pUnk);
    
        // get an interface to set the URL.
        CComPtr<IWebBrowser2> spBrowser;
        pUnk->QueryInterface(IID_IWebBrowser2, (void**)&spBrowser);
        if (spBrowser)
        {
            CComVariant ve;
            CComVariant vurl(pThreadParam->szURL);
    #pragma warning(disable: 4310) // cast truncates constant value
            spBrowser->put_Visible(VARIANT_TRUE);
    #pragma warning(default: 4310) // cast truncates constant value
            spBrowser->Navigate2(&vurl, &ve, &ve, &ve, &ve);
        }
    }
    
    To Build the Project edit and use Build.BAT present in the downloadable Zip file above.

    To run the Java Application, use "java MyWindow http://www.codeproject.com" from the command line.

    That's it! Have Fun.

    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

    Comments and Discussions

     
    GeneralReverse - JAva applet in c Pinmemberpsychospiller14-Dec-06 2:22 
    GeneralEmbed ActiveX controls inside Java GUI PinmemberShibaev Valera26-Nov-06 15:03 
    Generalinitialize() method call crashes Pinmemberndenkha16-May-06 14:09 
    GeneralCan't compile and build. Pinmemberndenkha16-May-06 8:55 
    GeneralProblem with not showing PinmembernameR228-Aug-05 2:00 
    GeneralUsing C++ Pinmembercberam12-Oct-04 0:50 
    GeneralNewJawin Source Code PinmemberArunsl1-Aug-04 20:35 
    GeneralIECanvas and JPanel Pinmembergigigigigi21-Jun-04 2:35 
    GeneralIECanvas Project Based on This Article PinsussAnonymous24-May-04 9:03 
    GeneralJava Applet in page: ERROR Pinmemberradim.keseg24-May-04 3:23 
    GeneralTab key doesn't work Pinmemberterryphillips14-May-04 0:39 
    Questioncan we use other light weight class instead of Canvas? PinsussAnonymous10-Sep-03 12:02 
    Generalunresolved external Pinmemberi_is_mike22-Jul-03 23:12 
    GeneralRe: unresolved external PinmemberRobinYang14-Nov-05 20:54 
    GeneralOther functionality of IE Pinmembersachinvshah15-Jul-03 2:30 
    GeneralUnsatisfiedLinkError PinmemberVipinTyagi13-Jul-03 22:00 
    GeneralError when exiting from Java Pinmemberbrighty27-Jun-03 4:13 
    QuestionCan this be done on Mac OS X? Pinmemberslwkf112-Jun-03 7:14 
    AnswerRe: Can this be done on Mac OS X? PinsussMac Guy17-Jun-03 11:00 
    GeneralRe: Can this be done on Mac OS X? Pinmemberslwkf117-Jun-03 16:29 
    Generalembedding on a Pocket PC Pinmembermarad2-May-03 5:09 
    GeneralRe: embedding on a Pocket PC Pinmemberrubao19-Nov-03 0:18 
    GeneralA problem of Dependencies is caused when deploying the project in VB .NET while application containing a third party ActiveX PinsussRiz Ali16-Apr-03 11:45 
    QuestionHow to get Hwnd with jdk 1.4 PinsussAnonymous13-Apr-03 21:59 
    AnswerRe: How to get Hwnd with jdk 1.4 Pinmembergusp14-Apr-03 5:39 

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

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

    | Advertise | Privacy | Mobile
    Web03 | 2.8.140721.1 | Last Updated 9 May 2000
    Article Copyright 2000 by Davanum Srinivas
    Everything else Copyright © CodeProject, 1999-2014
    Terms of Service
    Layout: fixed | fluid