Click here to Skip to main content
15,886,625 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

     
    GeneralRe: error on win32drawingsurface Pin
    brighty10-Apr-03 3:01
    brighty10-Apr-03 3:01 
    GeneralRe: error on win32drawingsurface Pin
    Txarli6-Jun-05 5:41
    Txarli6-Jun-05 5:41 
    Questionhow to use an existing activeX/com from java Pin
    Member 88162629-Oct-02 14:30
    Member 88162629-Oct-02 14:30 
    AnswerRe: how to use an existing activeX/com from java Pin
    sjava28-Feb-03 16:07
    sjava28-Feb-03 16:07 
    QuestionHow i embed an active x control in to java application ? Pin
    rizoriz27-Oct-02 18:43
    rizoriz27-Oct-02 18:43 
    AnswerRe: How i embed an active x control in to java application ? Pin
    bwishniak10-Apr-03 5:36
    bwishniak10-Apr-03 5:36 
    GeneralActiveX and MDI paradox Pin
    Anonymous20-Sep-02 2:55
    Anonymous20-Sep-02 2:55 
    QuestionRe: ActiveX and MDI paradox Pin
    snacker222-Apr-09 22:34
    snacker222-Apr-09 22:34 
    QuestionHow can I initialize my activeX control embeded with in Java GUI Pin
    Karthik Nataraj14-Sep-02 22:53
    Karthik Nataraj14-Sep-02 22:53 
    GeneralA Class is Missing Pin
    Amauw Scritz8-Sep-02 19:43
    Amauw Scritz8-Sep-02 19:43 
    GeneralRe: A Class is Missing Pin
    Karthik Nataraj14-Sep-02 21:42
    Karthik Nataraj14-Sep-02 21:42 
    GeneralRe: A Class is Missing Pin
    Anonymous13-Nov-02 11:38
    Anonymous13-Nov-02 11:38 
    GeneralRe: A Class is Missing Pin
    Pertti4-Mar-03 1:08
    Pertti4-Mar-03 1:08 
    GeneralRe: A Class is Missing Pin
    Member 16812927-Jan-03 19:46
    Member 16812927-Jan-03 19:46 
    GeneralRe: A Class is Missing Pin
    sjava28-Feb-03 16:08
    sjava28-Feb-03 16:08 
    GeneralFlash Movies In Java Pin
    Jeganbose Vincent5-Sep-02 23:40
    sussJeganbose Vincent5-Sep-02 23:40 
    GeneralRe: Flash Movies In Java Pin
    sjava31-Mar-03 16:28
    sjava31-Mar-03 16:28 
    GeneralGet the links and Text Values Associated with an applet. Pin
    Amauw Scritz1-Sep-02 20:15
    Amauw Scritz1-Sep-02 20:15 
    GeneralRe: Get the links and Text Values Associated with an applet. Pin
    brighty10-Apr-03 3:11
    brighty10-Apr-03 3:11 
    Questionhow to call a function in existing dll from java Pin
    15-Jun-02 23:14
    suss15-Jun-02 23:14 
    GeneralUnsatisfiedLinkError Pin
    15-May-02 6:09
    suss15-May-02 6:09 
    GeneralRe: UnsatisfiedLinkError Pin
    Irfan Dawood8-Sep-02 7:55
    Irfan Dawood8-Sep-02 7:55 
    GeneralRe: UnsatisfiedLinkError Pin
    Amauw Scritz8-Sep-02 17:26
    Amauw Scritz8-Sep-02 17:26 
    GeneralRe: UnsatisfiedLinkError Pin
    Member 16812927-Jan-03 19:50
    Member 16812927-Jan-03 19:50 
    GeneralTab Key not working Pin
    1-May-02 0:45
    suss1-May-02 0:45 

    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.