Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using JavaScript function to Override Java Method

331 views
Skip to first unread message

John Smith

unread,
Mar 26, 2000, 3:00:00 AM3/26/00
to
Hello All,
Is there a way in which one can override a Java method with a JavaScript
function ? What is the syntax for that ?
For example in this piece of code:
-----------------------------
function fnPaint(g) {
g.drawString("Hello Windows!",50.90);
}

function fnWindowEventsHandler(e)

switch (e.ID) {
case java.awt.event.WindowEvent.WINDOW_CLOSING:
java.util.System.out.println("WindowClosing at: " + Date())
System.exit(0);
default:
java.util.System.out.println("Unaccounted for Event at: " + Date())
}
}

var f = new java.awt.Frame("Hello Window");
f.setSize(200,200);
f.addWindowListener(WindowListener({windowClosing:
fnWindowEventsHandler }));
f.show();
++++++++++++++++++++++++
JavaScript function fnWindowEventsHandler is called when the window's close
box is clicked.
What I would like to do is to be able to specify, somehow, that my
JavaScript function fnPaint is to be called instead of the Java method paint
to paint the window.

I am playing with Rhino in this case.
Any suggestions, preferably polite, will be appreciated :-)

TIA


Patrick Beard

unread,
Mar 27, 2000, 3:00:00 AM3/27/00
to
In article <8bks0h$18...@secnews.netscape.com>, "John Smith"
<som...@microsoft.com> wrote:

> Hello All,
> Is there a way in which one can override a Java method with a JavaScript
> function ? What is the syntax for that ?

Well, if you're dealing with Java interfaces or abstract classes, you
can use Rhino's current extended syntax for that:

// Subclass an abstract class with a parameterless constructor:
//
// public abstract class Abstract {
// public abstract void method();
// }

var o = new Abstract() {
method: function() {}
};

However, if the class is neither abstract, nor an interface, Rhino can't
tell if you're sub-classing it or just passing a value to its
constructor. So in this case, you have to use the longer version of
this. So, here's an exapmple using a java.awt.Frame:

var painter = {
paint: function(g) {
var b = this.getBounds();
var x = b.x, y = b.y, w = b.width, h = b.height;
g.drawLine(x, y, x + w, y + h);
super$paint(g);
}
};
var f = new JavaAdapter(java.awt.Frame, painter);
f.setTitle("Painter Frame");

This long form can only be used if the class in question has a
parameterless constructor. Perhaps we should allow you to write an
"init()" method that would serve as the constructor.

--
// Patrick C. Beard
// Netscape Communications (for now)

John Smith

unread,
Mar 28, 2000, 3:00:00 AM3/28/00
to
Thanks a lot, Patrick. This appears to work in so far as I can see, i.e.
the line gets drawn and re-drawn when the window is re-sized. Alas, there
is, as seems inevitable, a 'minor' issue or two.
My code, gratefully adapted from your example, is like this (parts omitted
for brevity):
--- begin excerpt -----------------------------
var methodPaint = {

paint: function(g) {
var b = this.getBounds();
var x = b.x, y = b.y, w = b.width, h = b.height;
g.drawLine(x, y, x + w, y + h);
g.drawOval(30,30,60,30);
g.drawString(java.lang.String("Hello Windows!"),50.90);
super$paint(g);
}
};
// --------------------------------
var app = new JavaAdapter(Frame, methodPaint);
app.setTitle("Painter Frame");
app.setSize(200,200);
--- end excerpt ---

Whether running under JDK 1.1.7B or 1.2.2 (one at a time) I get:
-----------
Exception occurred during event dispatching:
org.mozilla.javascript.EvaluatorException: Can't find method
sun.java2d.SunGraphics2D.drawString(java.lang.String,number).
at
org.mozilla.javascript.DefaultErrorReporter.runtimeError(DefaultErrorReporte
r.java:61)
at
org.mozilla.javascript.Context.reportRuntimeError(Context.java:513)
at
org.mozilla.javascript.Context.reportRuntimeError(Context.java:529)
at
org.mozilla.javascript.NativeJavaMethod.call(NativeJavaMethod.java, Compiled
Code)
at
org.mozilla.javascript.ScriptRuntime.call(ScriptRuntime.java:1205)
at c3.call(test10.js:57)
at
org.mozilla.javascript.ScriptRuntime.call(ScriptRuntime.java:1205)
at
org.mozilla.javascript.JavaAdapter.callMethod(JavaAdapter.java:262)
at adapter0.paint(<adapter>)
at
sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:117)
at java.awt.Component.dispatchEventImpl(Component.java:2447)
at java.awt.Container.dispatchEventImpl(Container.java:1035)
at java.awt.Window.dispatchEventImpl(Window.java:749)
at java.awt.Component.dispatchEvent(Component.java:2307)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:287)
at
java.awt.EventDispatchThread.pumpOneEvent(EventDispatchThread.java:101)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:92)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:83)
----------
as soon as the window appears and the g.drawString... does not draw the
string and, the following method does not get executed, it seems.
When I comment out g.drawString(...) I get:
-----------
Exception occurred during event dispatching:
ReferenceError: "super$paint" is not defined.
at
org.mozilla.javascript.NativeGlobal.constructError(NativeGlobal.java:494)
at org.mozilla.javascript.ScriptRuntime.getBase(ScriptRuntime.java,
Compiled Code)
at c3.call(test10.js:58)
at
org.mozilla.javascript.ScriptRuntime.call(ScriptRuntime.java:1205)
at
org.mozilla.javascript.JavaAdapter.callMethod(JavaAdapter.java:262)
at adapter0.paint(<adapter>)
at
sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:117)
at java.awt.Component.dispatchEventImpl(Component.java:2447)
at java.awt.Container.dispatchEventImpl(Container.java:1035)
at java.awt.Window.dispatchEventImpl(Window.java:749)
at java.awt.Component.dispatchEvent(Component.java:2307)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:287)
at
java.awt.EventDispatchThread.pumpOneEvent(EventDispatchThread.java:101)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:92)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:83)
--------------------
The thing complains about the usage of super$paint(g).

Sorry to be a pain. I feel that, with the help you provided, I am getting
somewhere, however I don't, obviously, understand this enough to see what is
wrong and how to remedy the problem.

Thanks for your help.

Cheers

Patrick Beard <be...@netscape.com> wrote in message
news:beard-42E513....@news.mozilla.org...

Patrick Beard

unread,
Mar 28, 2000, 3:00:00 AM3/28/00
to
In article <8bqar9$l3...@secnews.netscape.com>, "John Smith"
<som...@microsoft.com> wrote:

> My code, gratefully adapted from your example, is like this (parts omitted
> for brevity):
> --- begin excerpt -----------------------------
> var methodPaint = {
> paint: function(g) {
> var b = this.getBounds();
> var x = b.x, y = b.y, w = b.width, h = b.height;
> g.drawLine(x, y, x + w, y + h);
> g.drawOval(30,30,60,30);
> g.drawString(java.lang.String("Hello Windows!"),50.90);
> super$paint(g);
> }
> };
> // --------------------------------
> var app = new JavaAdapter(Frame, methodPaint);
> app.setTitle("Painter Frame");
> app.setSize(200,200);
> --- end excerpt ---
>
> Whether running under JDK 1.1.7B or 1.2.2 (one at a time) I get:
> -----------
> Exception occurred during event dispatching:
> org.mozilla.javascript.EvaluatorException: Can't find method
> sun.java2d.SunGraphics2D.drawString(java.lang.String,number).

You've got a typo in your code here:

g.drawString(java.lang.String("Hello Windows!"),50.90);

You probably meant:

g.drawString(java.lang.String("Hello Windows!"),50, 90);

- Patrick

John Smith

unread,
Mar 29, 2000, 3:00:00 AM3/29/00
to
Hello Patriick,
You are absolutely correct. Time for me to get new eye glasses, I think :-)
Thanks a lot indeed for your assistance.
In addition to this fix I changed super$paint(g) to this.super$paint(g)
which fixed my other problem :-)
Thanks again for yoyr help and thanks to you and all the opther people
involved in Rhino.

Cheers


Patrick Beard <be...@netscape.com> wrote in message

news:beard-E98939....@news.mozilla.org...
> In article <8bqar9$l3...@secnews.netscape.com>, "John Smith"


> <som...@microsoft.com> wrote:
>
> > My code, gratefully adapted from your example, is like this (parts
omitted
> > for brevity):
> > --- begin excerpt -----------------------------
> > var methodPaint = {
> > paint: function(g) {
> > var b = this.getBounds();
> > var x = b.x, y = b.y, w = b.width, h = b.height;
> > g.drawLine(x, y, x + w, y + h);
> > g.drawOval(30,30,60,30);
> > g.drawString(java.lang.String("Hello Windows!"),50.90);
> > super$paint(g);
> > }
> > };
> > // --------------------------------
> > var app = new JavaAdapter(Frame, methodPaint);
> > app.setTitle("Painter Frame");
> > app.setSize(200,200);
> > --- end excerpt ---
> >
> > Whether running under JDK 1.1.7B or 1.2.2 (one at a time) I get:
> > -----------
> > Exception occurred during event dispatching:
> > org.mozilla.javascript.EvaluatorException: Can't find method
> > sun.java2d.SunGraphics2D.drawString(java.lang.String,number).
>

> You've got a typo in your code here:
>
> g.drawString(java.lang.String("Hello Windows!"),50.90);
>
> You probably meant:
>
> g.drawString(java.lang.String("Hello Windows!"),50, 90);
>
> - Patrick
>

renuka...@gmail.com

unread,
Sep 17, 2013, 6:07:50 AM9/17/13
to
JAVA online TRAINING: Java is that the basement for several applications that area unit currently leaders within the gift market. it's a growing profession, with large opportunities for promotion and high earnings. Java could be a hot topic in IT business and includes a bright future; java training is a perfect platform for anyone to find out java and continue to expertise a lot of advanced options of this technology. online training institutes created java training a simple task.

Learning online saves time and offers flexibility to all or any World Health Organization wish to find out and upgrade their pc skills. Hiring firms have access to all or any the comes that area unit done by students for java training and java certification, for those that secured smart score and affected in an exceedingly personal interview. online java training is as helpful and provides an equivalent pattern of learning that's followed in ancient room tutorials. increasing your information of java development and connected technologies nowadays with comprehensive, expert-led training from shalom IT college.

Online training pc courses don't seem to be restricted to the essential pc information only; instead folks area unit abundant interested learning through online for the advanced certified courses like Java. The java could be a platform and means of computing principally supported the ability of networks, and also the concept an equivalent code can run in several sets of computers, client gadgets and alternative devices. there's legion institutes provide java online training, however solely a top quality and licensed training is feasible with Shalom IT college. Shalom IT college with large expertise in online training stands firmly in providing online training for various folks all round the world. Well experience training is our expression, and training fee is cheap and 24/7 online support for the learners. Learning Java training offers a natural progression to spice up your career growth.

Online training pc courses don't seem to be restricted to the essential pc information only; instead folks area unit abundant interested learning through online for the advanced certified courses like Java. The java could be a platform and means of computing principally supported the ability of networks, and also the concept an equivalent code can run in several sets of computers, client gadgets and alternative devices. there's legion institutes provide java online training, however solely a top quality and licensed training is feasible with Shalom IT college. Shalom IT college with large expertise in online training stands firmly in providing online training for various folks all round the world. Well experience training is our expression, and training fee is cheap and 24/7 online support for the learners. Learning Java training offers a natural progression to spice up your career growth.

http://123trainings.com/it-adv-java-online-training.html
0 new messages