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
> 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)
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...
> 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
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
>