Click here to Skip to main content
Licence 
First Posted 8 May 2000
Views 336,051
Bookmarked 37 times

Embed ActiveX controls inside Java GUI

By | 8 May 2000 | Article
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

    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

    Member



    Sign Up to vote   Poor Excellent
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

     
    You must Sign In to use this message board. (secure sign-in)
     
    Search this forum  
     FAQ
        Noise  Layout  Per page   
      Refresh
    GeneralReverse - JAva applet in c Pinmemberpsychospiller2:22 14 Dec '06  
    GeneralEmbed ActiveX controls inside Java GUI PinmemberShibaev Valera15:03 26 Nov '06  
    Generalinitialize() method call crashes Pinmemberndenkha14:09 16 May '06  
    GeneralCan't compile and build. Pinmemberndenkha8:55 16 May '06  
    GeneralProblem with not showing PinmembernameR222:00 8 Aug '05  
    GeneralUsing C++ Pinmembercberam0:50 12 Oct '04  
    GeneralNewJawin Source Code PinmemberArunsl20:35 1 Aug '04  
    GeneralIECanvas and JPanel Pinmembergigigigigi2:35 21 Jun '04  
    GeneralIECanvas Project Based on This Article PinsussAnonymous9:03 24 May '04  
    GeneralJava Applet in page: ERROR Pinmemberradim.keseg3:23 24 May '04  
    GeneralTab key doesn't work Pinmemberterryphillips0:39 14 May '04  
    Questioncan we use other light weight class instead of Canvas? PinsussAnonymous12:02 10 Sep '03  
    Generalunresolved external Pinmemberi_is_mike23:12 22 Jul '03  
    GeneralRe: unresolved external PinmemberRobinYang20:54 14 Nov '05  
    GeneralOther functionality of IE Pinmembersachinvshah2:30 15 Jul '03  
    GeneralUnsatisfiedLinkError PinmemberVipinTyagi22:00 13 Jul '03  
    GeneralError when exiting from Java Pinmemberbrighty4:13 27 Jun '03  
    QuestionCan this be done on Mac OS X? Pinmemberslwkf17:14 12 Jun '03  
    AnswerRe: Can this be done on Mac OS X? PinsussMac Guy11:00 17 Jun '03  
    GeneralRe: Can this be done on Mac OS X? Pinmemberslwkf116:29 17 Jun '03  
    Generalembedding on a Pocket PC Pinmembermarad5:09 2 May '03  
    GeneralRe: embedding on a Pocket PC Pinmemberrubao0:18 19 Nov '03  
    GeneralA problem of Dependencies is caused when deploying the project in VB .NET while application containing a third party ActiveX PinsussRiz Ali11:45 16 Apr '03  
    QuestionHow to get Hwnd with jdk 1.4 PinsussAnonymous21:59 13 Apr '03  
    AnswerRe: How to get Hwnd with jdk 1.4 Pinmembergusp5:39 14 Apr '03  

    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.

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