Click here to Skip to main content
13,053,276 members (60,767 online)
Click here to Skip to main content
Add your own
alternative version

Stats

392.8K views
713 downloads
38 bookmarked
Posted 8 May 2000

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

    Share

    About the Author

    Davanum Srinivas
    United States United States
    No Biography provided

    You may also be interested in...

    Pro

    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
    gigigigigi21-Jun-04 2:35
    membergigigigigi21-Jun-04 2:35 
    GeneralIECanvas Project Based on This Article Pin
    Anonymous24-May-04 9:03
    sussAnonymous24-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
    sussAnonymous10-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
    sussMac 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
    Riz Ali16-Apr-03 11:45
    sussRiz Ali16-Apr-03 11:45 
    QuestionHow to get Hwnd with jdk 1.4 Pin
    Anonymous13-Apr-03 21:59
    sussAnonymous13-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.

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