Click here to Skip to main content
15,868,141 members
Articles / Desktop Programming / ATL
Article

Embed ActiveX controls inside Java GUI

Rate me:
Please Sign up or sign in to vote.
4.78/5 (18 votes)
8 May 2000 445.8K   858   39   106
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


    Written By
    United States United States
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

     
    GeneralReverse - JAva applet in c Pin
    psychospiller14-Dec-06 2:22
    psychospiller14-Dec-06 2:22 
    GeneralEmbed ActiveX controls inside Java GUI Pin
    Shibaev Valera26-Nov-06 15:03
    Shibaev Valera26-Nov-06 15:03 
    Generalinitialize() method call crashes Pin
    ndenkha16-May-06 14:09
    ndenkha16-May-06 14:09 
    GeneralCan't compile and build. Pin
    ndenkha16-May-06 8:55
    ndenkha16-May-06 8:55 
    GeneralProblem with not showing Pin
    nameR228-Aug-05 2:00
    nameR228-Aug-05 2:00 
    GeneralUsing C++ Pin
    cberam12-Oct-04 0:50
    cberam12-Oct-04 0:50 
    GeneralNewJawin Source Code Pin
    Arunsl1-Aug-04 20:35
    Arunsl1-Aug-04 20:35 
    GeneralIECanvas and JPanel Pin
    Member 59719721-Jun-04 2:35
    Member 59719721-Jun-04 2:35 
    Is there a way embedding IECanvas in JPanel or just putting IECanvas on the ContentPane of the JDialog directly ?
    GeneralIECanvas Project Based on This Article Pin
    Anonymous24-May-04 9:03
    Anonymous24-May-04 9:03 
    GeneralJava Applet in page: ERROR Pin
    radim.keseg24-May-04 3:23
    radim.keseg24-May-04 3:23 
    GeneralTab key doesn't work Pin
    terryphillips14-May-04 0:39
    terryphillips14-May-04 0:39 
    Questioncan we use other light weight class instead of Canvas? Pin
    Anonymous10-Sep-03 12:02
    Anonymous10-Sep-03 12:02 
    Generalunresolved external Pin
    i_is_mike22-Jul-03 23:12
    i_is_mike22-Jul-03 23:12 
    GeneralRe: unresolved external Pin
    RobinYang14-Nov-05 20:54
    RobinYang14-Nov-05 20:54 
    GeneralOther functionality of IE Pin
    sachinvshah15-Jul-03 2:30
    sachinvshah15-Jul-03 2:30 
    GeneralUnsatisfiedLinkError Pin
    VipinTyagi13-Jul-03 22:00
    VipinTyagi13-Jul-03 22:00 
    GeneralError when exiting from Java Pin
    brighty27-Jun-03 4:13
    brighty27-Jun-03 4:13 
    QuestionCan this be done on Mac OS X? Pin
    slwkf112-Jun-03 7:14
    slwkf112-Jun-03 7:14 
    AnswerRe: Can this be done on Mac OS X? Pin
    Mac Guy17-Jun-03 11:00
    Mac Guy17-Jun-03 11:00 
    GeneralRe: Can this be done on Mac OS X? Pin
    slwkf117-Jun-03 16:29
    slwkf117-Jun-03 16:29 
    Generalembedding on a Pocket PC Pin
    marad2-May-03 5:09
    marad2-May-03 5:09 
    GeneralRe: embedding on a Pocket PC Pin
    rubao19-Nov-03 0:18
    rubao19-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
    rizoriz16-Apr-03 11:45 
    QuestionHow to get Hwnd with jdk 1.4 Pin
    Anonymous13-Apr-03 21:59
    Anonymous13-Apr-03 21:59 
    AnswerRe: How to get Hwnd with jdk 1.4 Pin
    gusp14-Apr-03 5:39
    gusp14-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.