Re: opening a window from an applet..
- From: Frances <fdr58@xxxxxxxxx>
- Date: Mon, 29 Aug 2005 16:08:28 -0400
Thomas Hawtin wrote:
Frances wrote:
Thomas Hawtin wrote:
Frances wrote:
when you open a window from an applet (I mean JApplet) does that window also have to be an applet? if not then what? JFrame? (public class [class] extends JFrame?) if it's a JFrame instead of an applet does it still have to have init() method? or does it have to have main() method instead? I'm confused about this... can u pls point to where I can read up on this in java.sun.. (a lot of things they explain about applets is very different for JApplets.. and sometimes when they're explaining something about applets I don't know if what they're talking about also applies to JApplets..)
(can I open a swing window from an applet? or does it have to be from a JApplet?)
You have a few choices:
o Open a new browser window with AppletContext.showDocument (which might or might not work). In that case you would have a another applet embedded in the HTML page.
o Open a Frame/JFrame. Which is a frame like any other. Because you have a security manager installed, it'll have a little banner to notify the user that it is not to be trusted. There is no init or main methods in JFrame, and there doesn't need to be. Just create it and call setVisible(true) on it (from the Event Dispatch Thread). You don't even need to extend JFrame except in specialist cases. A problem with this is that you don't know where that applet is and nor do you have much control over z-order.
o Use JInternalFrames to create an MDI within the applet.
Tom Hawtin
thank you very much.. other problem I have is I don't know what Event-Dispatch Thread is (I saw this for the first time in java.sun, here..
http://java.sun.com/docs/books/tutorial/uiswing/components/applet.html
and well, still tyring to figure it out..
The code on that page looks reasonable.
Essentially the problem is witting multi-threaded code is very difficult. So, in Swing pretty much everything happens on the Event Dispatch Thread (EDT). To check that you are running in the EDT use java.awt.EventQueue.isDispatchThread().
(I'm tyring to open a new window from a JApplet, and, depending on content in the new window, the JApplet that's sup. to open the window can't find the class..
As in the link posted you should be creating components in the EDT. So to open a frame, say, do:
private JFrame frame;
@Override public void init() { try { java.awt.EventQueue.invokeAndWait(new Runnable() { public void run() { openFrame(); } }); } catch (InterruptedException exc) { // Ignore. } catch (InvocationTargetException exc) { throw new RuntimeException(exc); } } private static void openFrame() { assert java.awt.EventQueue.isDispatchThread(); if (frame != null) { frame = new JFrame("My frame"); frame.add(new JLabel("Hi Worl.")); frame.setDefaultCloseOperation( WindowConstants.DISPOSE_ON_CLOSE ); } frame.setVisible(true); } @Override public void destroy() { try { java.awt.EventQueue.invokeAndWait(new Runnable() { public void run() { if (frame != null) { frame.dispose(); frame = null; } } }); } catch (InterruptedException exc) { // Ignore. } catch (InvocationTargetException exc) { throw new RuntimeException(exc); } }
If applets had been introduced in 1.2 or even 1.1, I guess they would have used the EDT to call every method. Slightly obscurely, you should be able to get away with overriding addNotify instead (make sure to call super.addNotify()).
this is class I need to open in a new window from an applet..
http://www.francesdelrio.com/java/IM.java
(also, is window-opening code the same for a JApplet as it is for an applet?)
I would strong suggest not extending JFrame and putting as little as absolutely possible in the JApplet subclass.
Also use anonymous inner classes for listeners rather than attempting to have one class to tries to do everything.
I changed "extends JApplet" to "extends JFrame" in window I need to open, but the file that opens the window can't find this class.. and if I run class from command-line I get this error:
Exception in thread "main" java.lang.NoSuchMethodError: main
(which is why I asked if you have to have a main method if yr swing window is not an applet..)
An application will need a main method. That need have nothing to do with the JFrame class. So:
class MyApp { public static void main(String[] args) { // We need to be on the EDT. java.awt.EventQueue.invokeLater(new Runnable() { public void run() { openFrame(); } }); }
private static void openFrame() { assert java.awt.EventQueue.isDispatchThread(); JFrame frame = new JFrame("My frame"); frame.add(new JLabel("Hi Worl.")); frame.setDefaultCloseOperation( WindowConstants.DISPOSE_ON_CLOSE ); frame.setVisible(true); } }
You'll notice that the openFrame methods are very similar, and that we can easily write a program that runs as both an applet and an application easily.
Tom Hawtin
take that back.. WAS able to compile code you posted (hadn't imported necessary classes..) only one of them, though, on frame.java, get these two errors:
frame.java:7: 'class' or 'interface' expected
private JFrame frame;
^
frame.java:10: 'class' or 'interface' expected
public void init() {
^
2 errorsfirst line here is
private JFrame frame;
this looks like a var init.. don't see a class declaration here.. this is not a separate class? (the frame to be opened?)
MyApp compiles fine but when try to run ("java MyApp") get this error..
Exception in thread "main" java.lang.NoClassDefFoundError: MyApp/javaagain, many thanks.. Frances
.
- References:
- opening a window from an applet..
- From: Frances
- Re: opening a window from an applet..
- From: Thomas Hawtin
- Re: opening a window from an applet..
- From: Frances
- Re: opening a window from an applet..
- From: Thomas Hawtin
- opening a window from an applet..
- Prev by Date: Re: Render Fonts smoothly...
- Next by Date: Re: a strange problem regarding popup JDialogs/Windows
- Previous by thread: Re: opening a window from an applet..
- Next by thread: Re: opening a window from an applet..
- Index(es):
Relevant Pages
|
|