Click here to Skip to main content
14,977,142 members
Articles / Desktop Programming / ATL
Posted 8 May 2000


39 bookmarked

Embed ActiveX controls inside Java GUI

Rate me:
Please Sign up or sign in to vote.
4.78/5 (18 votes)
8 May 2000
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.
  • Download source and binary files - 6.9 Kb
  • Sample Image - javacom.gif


    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.

    // 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) 
            Win32DrawingSurface win32DrawingSurface = (Win32DrawingSurface)drawingSurfaceInfo.getSurface();
            hwnd = win32DrawingSurface.getHWnd();
        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.

    // Creates IE control
    VOID CreateIEControl(ThreadParam *pThreadParam)
        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",
        IUnknown *pUnk = NULL;
        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
    #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" from the command line.

    That's it! Have Fun.


    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 Pin
    psychospiller14-Dec-06 2:22
    Memberpsychospiller14-Dec-06 2:22 
    GeneralEmbed ActiveX controls inside Java GUI Pin
    Shibaev Valera26-Nov-06 15:03
    MemberShibaev Valera26-Nov-06 15:03 
    Generalinitialize() method call crashes Pin
    ndenkha16-May-06 14:09
    Memberndenkha16-May-06 14:09 
    GeneralCan't compile and build. Pin
    ndenkha16-May-06 8:55
    Memberndenkha16-May-06 8:55 
    GeneralProblem with not showing Pin
    nameR228-Aug-05 2:00
    MembernameR228-Aug-05 2:00 
    GeneralUsing C++ Pin
    cberam12-Oct-04 0:50
    Membercberam12-Oct-04 0:50 
    GeneralNewJawin Source Code Pin
    Arunsl1-Aug-04 20:35
    MemberArunsl1-Aug-04 20:35 
    GeneralIECanvas and JPanel Pin
    Member 59719721-Jun-04 2:35
    MemberMember 59719721-Jun-04 2:35 
    GeneralIECanvas Project Based on This Article Pin
    Anonymous24-May-04 9:03
    MemberAnonymous24-May-04 9:03 
    GeneralJava Applet in page: ERROR Pin
    radim.keseg24-May-04 3:23
    Memberradim.keseg24-May-04 3:23 
    GeneralTab key doesn't work Pin
    terryphillips14-May-04 0:39
    Memberterryphillips14-May-04 0:39 
    Questioncan we use other light weight class instead of Canvas? Pin
    Anonymous10-Sep-03 12:02
    MemberAnonymous10-Sep-03 12:02 
    Generalunresolved external Pin
    i_is_mike22-Jul-03 23:12
    Memberi_is_mike22-Jul-03 23:12 
    GeneralRe: unresolved external Pin
    RobinYang14-Nov-05 20:54
    MemberRobinYang14-Nov-05 20:54 
    GeneralOther functionality of IE Pin
    sachinvshah15-Jul-03 2:30
    Membersachinvshah15-Jul-03 2:30 
    GeneralUnsatisfiedLinkError Pin
    VipinTyagi13-Jul-03 22:00
    MemberVipinTyagi13-Jul-03 22:00 
    GeneralError when exiting from Java Pin
    brighty27-Jun-03 4:13
    Memberbrighty27-Jun-03 4:13 
    QuestionCan this be done on Mac OS X? Pin
    slwkf112-Jun-03 7:14
    Memberslwkf112-Jun-03 7:14 
    AnswerRe: Can this be done on Mac OS X? Pin
    Mac Guy17-Jun-03 11:00
    MemberMac Guy17-Jun-03 11:00 
    GeneralRe: Can this be done on Mac OS X? Pin
    slwkf117-Jun-03 16:29
    Memberslwkf117-Jun-03 16:29 
    Generalembedding on a Pocket PC Pin
    marad2-May-03 5:09
    Membermarad2-May-03 5:09 
    GeneralRe: embedding on a Pocket PC Pin
    rubao19-Nov-03 0:18
    Memberrubao19-Nov-03 0:18 
    GeneralA problem of Dependencies is caused when deploying the project in VB .NET while application containing a third party ActiveX Pin
    rizoriz16-Apr-03 11:45
    Memberrizoriz16-Apr-03 11:45 
    QuestionHow to get Hwnd with jdk 1.4 Pin
    Anonymous13-Apr-03 21:59
    MemberAnonymous13-Apr-03 21:59 
    AnswerRe: How to get Hwnd with jdk 1.4 Pin
    gusp14-Apr-03 5:39
    Membergusp14-Apr-03 5:39 

    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.