Click here to Skip to main content
15,886,830 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 447.7K   859   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

     
    Generalpackage Pin
    28-Apr-02 18:50
    suss28-Apr-02 18:50 
    QuestionCStatusBar ? Pin
    25-Oct-01 7:06
    suss25-Oct-01 7:06 
    Questionhow to access the DWebBrowserEvents Pin
    22-Sep-01 21:20
    suss22-Sep-01 21:20 
    QuestionHow can i embedd Word in Java Program ? Pin
    8-Sep-01 1:45
    suss8-Sep-01 1:45 
    AnswerRe: How can i embedd Word in Java Program ? Pin
    5-Apr-02 10:46
    suss5-Apr-02 10:46 
    AnswerRe: How can i embedd Word in Java Program ? Pin
    sjava31-Mar-03 16:29
    sjava31-Mar-03 16:29 
    GeneralVB Dll from Java Pin
    xanshu14-Aug-01 7:17
    xanshu14-Aug-01 7:17 
    GeneralRe: VB Dll from Java Pin
    31-Oct-01 19:12
    suss31-Oct-01 19:12 
    GeneralRe: VB Dll from Java Pin
    George Zheng14-Nov-01 9:41
    George Zheng14-Nov-01 9:41 
    GeneralRe: VB Dll from Java Pin
    5-Apr-02 10:03
    suss5-Apr-02 10:03 
    GeneralProblem during program termination Pin
    Gan3-Jul-01 22:36
    Gan3-Jul-01 22:36 
    GeneralRegarding an Error Pin
    Krithivasan M. S24-Jun-01 19:27
    Krithivasan M. S24-Jun-01 19:27 
    Generaljava and SOAP Pin
    16-May-01 5:08
    suss16-May-01 5:08 
    GeneralUsing in an internalframe Pin
    4-Apr-01 6:45
    suss4-Apr-01 6:45 
    GeneralRe: Using in an internalframe Pin
    Soundari11-Oct-01 0:46
    Soundari11-Oct-01 0:46 
    Questionhow to show ohter URL? Pin
    14-Mar-01 19:42
    suss14-Mar-01 19:42 
    AnswerRe: how to show ohter URL? Pin
    3-Feb-02 2:31
    suss3-Feb-02 2:31 
    GeneralJAVA Pin
    7-Mar-01 17:47
    suss7-Mar-01 17:47 
    GeneralRe: JAVA Pin
    29-May-02 3:44
    suss29-May-02 3:44 
    QuestionHow can i exit this program?? Pin
    19-Dec-00 17:05
    suss19-Dec-00 17:05 
    AnswerRe: How can i exit this program?? Pin
    16-Feb-01 3:55
    suss16-Feb-01 3:55 
    GeneralDeprecation Problems Pin
    Bill21-Sep-00 23:59
    Bill21-Sep-00 23:59 
    GeneralRe: Deprecation Problems Pin
    Davanum Srinivas22-Sep-00 2:06
    Davanum Srinivas22-Sep-00 2:06 
    GeneralRe: Deprecation Problems Pin
    Alex Rose5-Jun-01 8:53
    Alex Rose5-Jun-01 8:53 
    GeneralNice Pin
    blueopera15-Aug-00 11:35
    blueopera15-Aug-00 11:35 

    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.