Solved

Running Java applet from Delphi

Posted on 2003-11-30
3
623 Views
Last Modified: 2010-04-06
I need to run a java .jar file from my Delphi 7 application.  I have not used java before. What are the options for calling the .jar file?
0
Comment
Question by:mcmahling
3 Comments
 
LVL 7

Accepted Solution

by:
sftweng earned 500 total points
ID: 9846988
I recommend that you look at the JEDI site - they have release (free) the JCL and JVCL component libraries - among them is a "Shell" unit called JclShell that makes it easy to invoke the underlying Windows shell capabilities, including running an application. You can find them at http://www.delphi-jedi.org/
0
 
LVL 2

Expert Comment

by:insomniac92
ID: 9847439
I would suggest embedding the TWebBrowser component and loading the Java Applet into that. Here is a cut down version of a component I wrote a while ago using a TPageProducer and the TWebBrowser component . I have just stripped out a lot of extra stuff without re-compiling it or testing it but it should give you enough of an idea of how to do this.
//component unit starts here...

unit JavaAppletComponent;

interface

uses
  SysUtils, Messages, Classes, Controls, HTTPApp, DsgnIntf, FileCtrl, ActiveX,
  ComObj, SHDocVw, extctrls, Forms, olectrls, dialogs;

type
  TJavaAppletComponent = class(TCustomPanel)
  private
    FAppletPath            : string;
    FAppletFile            : string;
    FAppletParams          : TStrings;
    FWebBrowser            : TWebBrowser;
    FActivate              : boolean;
    FAutoSize              : boolean;
    FPageProducer          : TPageProducer;
    FHTMLFile1, FHTMLFile2 : string;

    procedure SetAppletParams(Value: TStrings);
    procedure SetActive(Value: boolean);
    procedure SetAppletPath(Value: string);
    procedure SetAppletFile(Value: string);
    procedure SetAutoSize(Value: Boolean);

  protected

  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    property AppletPath: string read FAppletPath write SetAppletPath;
    property Canvas;

  published
    property AppletParams: TStrings read FAppletParams write SetAppletParams;
    property Active: boolean read FActivate write SetActive;
    property AppletFile: string read FAppletFile write SetAppletFile;
    property AutoSize: boolean read FAutoSize write SetAutoSize;
    property Caption;
    property BevelInner;
    property BevelOuter;
    property BevelWidth;
    property BorderStyle;
    property BorderWidth;
    property Color;
    property Cursor;
    property Hint;
    property ShowHint;
    property TabStop;
    property TabOrder;
    property Visible;
    property OnMouseDown;
    property OnMouseMove;
    property OnMouseUp;
    property OnDragOver;
    property OnEndDrag;
    property OnDragDrop;
    property OnStartDrag;
  end;

  TAppletFileNameProperty = class(TStringProperty)
  public
    procedure Edit; override;
    function GetAttributes: TPropertyAttributes; override;
  end;


procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('InteracTV', [TJavaAppletComponent]);
  RegisterPropertyEditor(TypeInfo(string), TJavaAppletComponent, 'AppletFile', TAppletFileNameProperty);
end;

constructor TJavaAppletComponent.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  Width := 65;
  Height := 65;
  Caption := '';
  BevelOuter := bvNone;
  BevelInner := bvNone;
  BorderStyle := bsNone;
  FAppletParams := TStringList.Create;
  FPageProducer := TPageProducer.Create(Self);
  FPageProducer.Dispatcher := nil;
  FPageProducer.HTMLDoc.Clear;
  FPageProducer.HTMLFile := '';
  FWebBrowser := TWebBrowser.Create(Self);
  FWebBrowser.Align := alClient;
  FWebBrowser.Silent := True;
  FWebBrowser.MenuBar := False;
  FWebBrowser.AddressBar := False;
  FWebBrowser.StatusBar := False;
  FWebBrowser.TheaterMode := False;
  FWebBrowser.RegisterAsBrowser := False;
  FWebBrowser.RegisterAsDropTarget := True;
  FWebBrowser.TabStop := FALSE; //
  FWebBrowser.ToolBar := -1;
  FWebBrowser.OffLine := False;
  Self.InsertControl(FWebBrowser);
  TOleControl(FWebBrowser).Visible := False;
  Invalidate;
end;

destructor TJavaAppletComponent.Destroy;
begin
//  if FHTMLFile1 <> '' then
//    DeleteFile(PChar(FHTMLFile1));
//  if FHTMLFile2 <> '' then
//    DeleteFile(PChar(FHTMLFile2));
  FAppletParams.Free;
  if FWebBrowser <> nil then
  begin
    FWebBrowser.Free;
    FWebBrowser := nil;
  end;
  FPageProducer.free;
  FPageProducer := nil;
  inherited Destroy;
end;


procedure TJavaAppletComponent.SetAutoSize(Value: boolean);
var
 i, z, wPos, hPos : integer;
 wStr, hStr, sWidth, sHeight : string;
 WidthFound, HeightFound : Boolean;
begin
  FAutoSize := Value;
  if FAutoSize = True then
  begin
    if FAppletParams.Count > 0 then
    begin
      wPos := 0;
      hPos := 0;
      for i := 0 to FAppletParams.Count - 1 do
      begin
        if wPos = 0 then
        begin
          wPos := Pos('WIDTH', UpperCase(FAppletParams.Strings[i]));
          if wPos > 0 then wstr := Copy(FAppletParams.Strings[i], (wPos + 5), (Length(FAppletParams.Strings[i]) - wPos));
        end;
        if hPos = 0 then
        begin
          hPos := Pos('HEIGHT', UpperCase(FAppletParams.Strings[i]));
          if hPos > 0 then hstr := Copy(FAppletParams.Strings[i], (hPos + 6), (Length(FAppletParams.Strings[i]) - hPos));
        end;
      end;
      if wPos > 0 then
       begin
         WidthFound := False;
         i := 0;
         while (i < (Length(wStr) - 1)) and (not WidthFound) do
         begin
           if wStr[i] = '"' then
            begin
              for z := i + 1 to Length(wStr) - 1 do
              begin
                if WidthFound = False then
                 begin
                  if wStr[z] <> '"' then sWidth := sWidth + wStr[z];
                  if wStr[z] = '"' then WidthFound := True;
                 end;
              end;
            end;
            inc(i);
         end;
         if WidthFound then Self.Width := StrToInt(sWidth);
       end;

      if hPos > 0 then
       begin
         HeightFound := False;
         i := 0;
         while (i < (Length(hStr) - 1)) and (not HeightFound) do
         begin
           if hStr[i] = '"' then
            begin
              for z := i + 1 to Length(hStr) - 1 do
              begin
                if HeightFound = False then
                 begin
                  if hStr[z] <> '"' then sHeight := sHeight + hStr[z];
                  if hStr[z] = '"' then HeightFound := True;
                 end;
              end;
            end;
            inc(i);
         end;
         if HeightFound then Self.Height := StrToInt(sHeight);
       end;
    end;
  end;
  Invalidate;
end;

procedure TJavaAppletComponent.SetAppletPath(Value: string);
begin
  if FAppletPath <> Value then
  begin
    FAppletPath := Value;
    Invalidate;
  end;
end;

procedure TJavaAppletComponent.SetAppletFile(Value: string);
var
 i, j : integer;
 s1 : string;
begin
  if FAppletFile <> Value then
  begin
     FAppletFile := Value;
     if FAppletFile <> '' then
     begin
       for i := 0 to Length(FAppletFile) do
       begin
         if FAppletFile[i] = '\' then j := i;
       end;
       FAppletPath := Copy(FAppletFile, 0, j - 1);
     end;
     Invalidate;
  end;
end;

procedure TJavaAppletComponent.SetActive(Value: Boolean);
var
 wFlags            : OleVariant;
 wTargetFrameName  : OleVariant;
 wPostData         : OleVariant;
 wHeaders          : OleVariant;
 i                 : integer;
 ipos              : integer;
 v                 : variant;
begin
  if Value <> FActivate then
  begin
    FActivate := Value;
    case FActivate of
      TRUE:
        begin
          if FAppletParams.Count= 0 then
          begin
            FActivate := False;
            showmessage('Error: Applet Params not set.');
            exit;
          end;
          if FAppletFile = '' then
          begin
            FActivate := False;
            showmessage('Error: Applet File property not set.');
            exit;
          end;
          if not fileExists(FAppletFile) then
          begin
            FActivate := False;
            showmessage('Unable to locate Applet file.');
            exit;
          end;

            if FAutoSize = True then SetAutoSize(True);
            FPageProducer.HTMLDoc.Clear;
            FPageProducer.HTMLDoc.Add('<body topmargin="0" leftmargin="0" rightmargin="0" bottomargin="0" SCROLL="no">');
            for i := 0 to FAppletParams.Count - 1 do
            begin
              FPageProducer.HTMLDoc.Add(FAppletParams.Strings[i]);
            end;
            FPageProducer.HTMLDoc.Add('</applet>');
            FPageProducer.HTMLDoc.Add('<body/>');
            FPageProducer.HTMLDoc.SaveToFile(FAppletPath + '\' + inttostr(Self.handle) + '.html');
            FHTMLFile1 := FAppletPath + '\' + inttostr(Self.handle) + '.html';
            wFlags := 2;
            wPostData := False;
            FWebBrowser.Navigate(FAppletPath + '\' + inttostr(Self.handle) + '.html', wFlags, wTargetFrameName, wPostData, wHeaders);
            TOleControl(FWebBrowser).Visible := True;
          Self.Visible := True;
        end;

      FALSE:
        begin
          TOleControl(FWebBrowser).Visible := False;
        end;
    end;
    Invalidate;
  end;
end;

procedure TJavaAppletComponent.SetAppletParams(Value: TStrings);
begin
  if Value <> FAppletParams then
  begin
    FAppletParams.Assign(Value);
    if FAutoSize = True then SetAutoSize(True);
    Invalidate;
  end;
end;

procedure TAppletFilenameProperty.Edit;
var
  Dialog: TOpenDialog;
begin
  Dialog := TOpenDialog.create(Application);
  Dialog.Filter := 'Class Names (*.class)|*.CLASS';
  Dialog.Title := 'Select an Applet File...';
  Dialog.Options := [ofHideReadOnly,ofPathMustExist,ofFileMustExist];
  Dialog.Execute;
  SetStrValue(Dialog.FileName);
  SetValue(Dialog.FileName);
  Dialog.Free;
end;

function TAppletFilenameProperty.GetAttributes: TPropertyAttributes;
begin
  Result := [paDialog];
end;

end.

//end of component unit....

Cheers

0
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 9848381
If you want to know something about the Java Native Interface then have a look here
http://www.pacifier.com/~mmead/jni/delphi/
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now