java - Return control to an applet -
i have japplet during "init" starts jframe. there operations in jframe , user closes jframe finally.
now i'd notify browser via javascript done.
how accomplish that? if applet alone, simple function below enough
public void notifybrowser() { jsobject browserwindow = jsobject.getwindow(this); browserwindow.eval("try{refreshfilesinlongue();}catch(e){alert('error');}"); }
but "this" in code referring applet object, , don't know how reach object while in jframe.
jframe called below in order receive focus:
public void init() { paramposturl = this.getparameter("posturl"); /* create , display applet */ try { java.awt.eventqueue.invokeandwait(new runnable() { public void run() { initcomponents(); scanframe scanframe = new scanframe(); scanframe.setposturl(paramposturl); scanframe.setvisible(true); try { thread.sleep(300l); scanframe.tofront(); } catch (interruptedexception ex) { logger.getlogger(applet.class.getname()).log(level.severe, null, ex); } } }); } catch (exception ex) { ex.printstacktrace(); } }
so questions are:
1. how can call notifybrowser(), while being in scanframe object
2. or how detect scanframe closed , being in applet object call notifybrowser?
thanks help.
so modified code that:
public class applet extends javax.swing.japplet { ... public void init() { paramposturl = this.getparameter("posturl"); myapplet = this; /* create , display applet */ try { java.awt.eventqueue.invokeandwait(new runnable() { @override public void run() { initcomponents(); scanframe scanframe = new scanframe(myapplet); ...
and scanframe
public class scanframe extends javax.swing.jframe implements scannerlistener { ... private japplet appletobj; public scanframe(japplet myapplet) { appletobj = myapplet; .... private void sendscanneddocumenttocallingapp() { .... jsobject browserwindow; browserwindow = jsobject.getwindow((japplet)appletobj); browserwindow.eval("try{refreshfilesinlongue();}catch(e){alert('error');}"); }
and error get:
exception in thread "awt-eventqueue-2" java.util.nosuchelementexception @ java.util.linkedlist.getfirst(unknown source) @ java.awt.sequencedevent.getfirst(unknown source) @ java.awt.sequencedevent.dispatch(unknown source) @ java.awt.eventqueue.dispatcheventimpl(unknown source) @ java.awt.eventqueue.access$200(unknown source) @ java.awt.eventqueue$3.run(unknown source) @ java.awt.eventqueue$3.run(unknown source) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(unknown source) @ java.security.protectiondomain$1.dointersectionprivilege(unknown source) @ java.awt.eventqueue$4.run(unknown source) @ java.awt.eventqueue$4.run(unknown source) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(unknown source) @ java.awt.eventqueue.dispatchevent(unknown source) @ java.awt.eventdispatchthread.pumponeeventforfilters(unknown source) @ java.awt.eventdispatchthread.pumpeventsforfilter(unknown source) @ java.awt.eventdispatchthread.pumpeventsforhierarchy(unknown source) @ java.awt.eventdispatchthread.pumpevents(unknown source) @ java.awt.eventdispatchthread.pumpevents(unknown source) @ java.awt.eventdispatchthread.run(unknown source)
- if need open separate window, don't use jframe. use modal jdialog, , control returns automatically calling code.
- if need pass japplet's reference class, pass in parameter of constructor.
i.e.,
scanframe scanframe = new scanframe(this);
where this
represents japplet.
note calling thread.sleep(300l);
on swing event thread bad idea. if need delay this, use swing timer don't shut down event thread during sleep.
edit
state,
i copied stuff found in internet,
be careful here. copy ideas not code lest run unseen walls.
...but suspected not way go, although didn't notify problems until , solved issue of jframe going behind browser window. question is, when pass applet object via this, how can call , function notifybrowser. netbeans told me change scanframe constructor this: public scanframe(runnable athis)
your japplet class apparently implements runnable should extend japplet. note netbeans suggestion done out of ignorance of plans. ide smart, so smart. know better constructor parameter should japplet can call japplet methods on parameter.
public class scanframe { private japplet myapplet; public scanframe(japplet myapplet) { this.myapplet = myapplet; // .... etc... } }
and can call applet methods on myapplet field.
edit 2
ah, bad, forgot calling constructor anonymous inner class. let me change recommendations:
- first rename class. "applet" name core java class of importance, , don't want use may confuse others or doing this. let's rename scanframeapplet.
- then
this
of instance of applet inside of anonymous inner class, must prefacethis
class name, in instancescanframeapplet.this
.
i.e.,
public class scanframeapplet extends japplet { try { java.awt.eventqueue.invokeandwait(new runnable() { @override public void run() { initcomponents(); scanframe scanframe = new scanframe(scanframeapplet.this);
Comments
Post a Comment