Jump to content
Ranja AZ

Need help converting java instruction to delphi

Recommended Posts

Here is an instruction that I cannot convert to delphi:

 

                printer.startPrint(false, new OnPrintListener() {       //roll paper or not
                    @Override
                    public void onPrintResult(final int retCode) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(PrinterActivity.this, retCode + "", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                });

Regard.

Share this post


Link to post

Thank you for your reply!

I have a java file (attached file) and I have java4delphi, can I convert this java file to .pas file with this tools? Please How to do that?

 

Regard.

PrinterActivity.java

Share this post


Link to post
Guest

You need to state your "environment". And what you are trying to achieve. It's not very clear.

 

Do you want to writ Pascal and execute it as JS?

Do you want to convert (transplie) Pascal to JS and run the JS in... webbrowser or node.js?

 

What do you want to do?

First you ask "Pascal => Java".

Then you state "i have a jar".

 

Witch way? What is your needs? What are you doing? At all?

Share this post


Link to post
4 hours ago, Ranja AZ said:

can I convert this java file to .pas file with this tools?

Unlikely. Such tools are to create imports from .jar or .class files, not convert Java code to Delphi. 

Your main problem will be whether OnPrintListener is a class or an interface, because you cannot implement a descendant of a Java class in Delphi - it needs to be done in Java. Java interfaces however, can be implemented in Delphi by creating a descendant of TJavaLocal, and implementing the interface in question. There are a number of examples of descendants of TJavaLocal throughout the Delphi source.

 

In order to determine what OnPrintListener is, you could use the aforementioned tools to import the Java classes from the .jar that you mentioned, assuming that it is the one that contains com.nexgo.oaf.apiv3.

Share this post


Link to post

I want to convert java code to delphi code.

 

My problem is the call to new OnPrintListener () in the parameter.

 

Regard

Share this post


Link to post
22 hours ago, Ranja AZ said:

I want to convert java code to delphi code.

It appears you have already asked a similar question, here: https://stackoverflow.com/questions/65345853/is-it-possible-to-write-the-following-java-program-in-delphi

The principal for your present issue is identical, i.e. if OnPrintListener is a class, then you. cannot do it in Delphi. If it's an interface (which, given your other question, it's likely), then you already have an example of how to implement a Java interface, in the StackOverflow answer. A possible example for this case:

uses
  Androidapi.JNIBridge, Androidapi.JNI.JavaTypes, Androidapi.Helpers;

type
  JOnPrintListener = interface;

  JOnPrintListenerClass = interface(IJavaClass)
    ['{076904DD-77D9-497D-BA21-992A9B8FED3E}']
  end;

  [JavaSignature('com.nexgo.oaf.apiv3.device.printer.OnPrintListener')]
  JOnPrintListener = interface(IJavaInstance)
    ['{F688775D-067F-4521-A045-9106599F4C4A}']
    procedure onPrintResult(retCode: Integer); cdecl;
    // *** NOTE *** There may be other methods for this interface
  end;
  TJOnPrintListener = class(TJavaGenericImport<JOnPrintListenerClass, JOnPrintListener>) end;

  TRunnable = class(TJavaLocal, JRunnable)
  private
    FCallback: TProc;
  public
    { JRunnable }
    procedure run; cdecl;
  public
    constructor Create(const ACallback: TProc);
  end;

  TPrintListener = class(TJavaLocal, JOnPrintListener)
  private
    FRetCode: Integer;
    FRunnable: JRunnable;
    procedure DoRun;
  public
    { JOnPrintListener }
    procedure onPrintResult(retCode: Integer); cdecl;
  public
    constructor Create;
  end;

{ TRunnable }

constructor TRunnable.Create(const ACallback: TProc);
begin
  inherited Create;
  FCallback := ACallback;
end;

procedure TRunnable.run;
begin
  FCallback;
end;

{ TPrintListener }

constructor TPrintListener.Create;
begin
  inherited;
  FRunnable := TRunnable.Create(DoRun);
end;

procedure TPrintListener.DoRun;
begin
  // Do the toast thing here, using FRetCode
end;

procedure TPrintListener.onPrintResult(retCode: Integer);
begin
  FRetCode := retCode;
  TAndroidHelper.Activity.runOnUiThread(FRunnable);
end;

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
  // Assuming FPrintListener is defined as: FPrintListener: JOnPrintListener
  FPrintListener := TPrintListener.Create;
  // Assuming you have created an instance of Printer (imported as JPrinter) called FPrinter
  FPrinter.startPrint(False, FPrintListener);
end;

Note all of the assumptions being made - the biggest one being that OnPrintListener is an interface. Also I don't have the .jar, nor the printer, so of course it is untested

 

  • Like 3

Share this post


Link to post

Thank you Dave!

 

Your explanation is very clear. Now printing is OK now. Thank you so much.

 

Regard 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×