- If you need to open a separate window, don't use a JFrame. Use a modal JDialog, and then control returns automatically to the calling code.
- If you need to pass the JApplet's reference into another class, simply pass it in as a parameter of the constructor.
i.e.,
ScanFrame scanFrame = new ScanFrame(this);
where this
represents the JApplet.
Note that calling Thread.sleep(300L);
on the Swing event thread is a very bad idea. If you need a delay like this, use a Swing Timer so you don't shut down the event thread during the sleep.
Edit
You state,
I just copied some stuff I found in the Internet,
Be careful here. Copy ideas but not code lest you run into unseen walls.
...but I suspected it was not the way to go, although I didn't notify any problems with that until now and it solved the issue of the JFrame going behind the browser window. But the question is, when I pass the applet object via this, how can I call it and my function notifyBrowser. My Netbeans told me to change ScanFrame constructor to something like this: public ScanFrame(Runnable aThis)
Your JApplet class apparently implements Runnable but it also should extend JApplet. Note that the NetBeans suggestion is done out of ignorance of your plans. An IDE is smart, but only so smart. You know better that the constructor parameter should be a JApplet so that you can call JApplet methods on the parameter.
public class ScanFrame {
private JApplet myApplet;
public ScanFrame(JApplet myApplet) {
this.myApplet = myApplet;
// .... etc...
}
}
and now you can call applet methods on the myApplet field.
Edit 2
Ah, my bad, I forgot that you were calling the constructor from an anonymous inner class. Let me change some recommendations:
- First rename your class. "Applet" is already a name for a core Java class of some importance, and you don't want to use it as you may confuse others or yourself by doing this. Let's say you rename it to ScanFrameApplet.
- Then to get the
this
of the instance of this applet inside of an anonymous inner class, you must preface thethis
with the class name, so in my instance it would beScanFrameApplet.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);