>>> 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
[quoted text clipped - 31 lines]
> 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
Frances - 29 Aug 2005 19:33 GMT
>>>> 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
[quoted text clipped - 144 lines]
>
> Tom Hawtin
thank you very much ... I cannot believe how complicated it is to open
a new window from an applet (not new App, new window..) this is simply
for a chat app applet, chat window opens in a new window (new window
doesn't mean a separate app.. just means user opens chat app, logs in,
chat window opens..) I think I may not have made myself clear..
I already wrote my chatApp interface, now have to change a whole lot to
open from applet main window (you know, like a splash window..) :(
inner classes...
EDT..
ok, need to figure all this out... (so what is now in init() method in
my chat window interface now goes in an inner class?)
regarding EDT.. from pg previously mentioned..
"Swing components should be created, queried, and manipulated on the
event-dispatching thread, but browsers don't invoke applet "milestone"
methods from that thread. For this reason, the milestone methods — init,
start, stop, and destroy — should use the SwingUtilities method
invokeAndWait (or, if appropriate, invokeLater)"
now these invokeAndWait and invokeLater -- are THESE the event-handling
threads?
(I was not able to compile code you posted (MyApp.java and frame.java..)
I do understand basic concept of threads (to run several processes at
the same time, let's say several connections from a client to a server,
or several simultaneous connections to a db...) but I don't understand
why you need to do this to display Swing GUI elements..
again, thank you very much for your time..
Thomas Hawtin - 29 Aug 2005 23:10 GMT
> now these invokeAndWait and invokeLater -- are THESE the event-handling
> threads?
> (I was not able to compile code you posted (MyApp.java and frame.java..)
These are methods that take a Runnable object and pass it over to the
EDT where it is run.
The difference between the two is that invokeLater just sends the task
off to be run but returns immediately, whereas invokeAndWait sends the
task off, waits for it to complete and transfers any exception to the
calling thread.
> I do understand basic concept of threads (to run several processes at
> the same time, let's say several connections from a client to a server,
> or several simultaneous connections to a db...) but I don't understand
> why you need to do this to display Swing GUI elements..
If you had multiple threads all accessing the same objects at the same
time you'll get a mess. Having one thread to access a bunch of objects
is equivalent to throwing a big synchronize around the lot for every
possible route to access them.
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
Frances - 29 Aug 2005 21:08 GMT
>>>> 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
[quoted text clipped - 144 lines]
>
> 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 errors
first 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/java
again, many thanks.. Frances
>thank you very much.. other problem I have is I don't know what
>Event-Dispatch Thread
It is the default thread in AWT or Swing, the one that triggers
events. If you don't want to be using that thread, you had better
have spawned your own Thread.
See http://mindprod.com/jgloss/thread.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
Frances - 31 Aug 2005 18:53 GMT
>>thank you very much.. other problem I have is I don't know what
>>Event-Dispatch Thread
[quoted text clipped - 4 lines]
>
> See http://mindprod.com/jgloss/thread.html
thank you very much the Thomases, the Andrews, Roedy, for your help.. I
finally got my window to open from a JApplet..
window OPENED:
public class ChatRoom extends JFrame implements ActionListener,
KeyListener {
// decl vars...
ChatRoom(String title) {
super(title);
init();
}
code that opens this window:
public class oWin extends JApplet implements ActionListener {
JFrame window;
.....
public void init() {
....
window = new ChatRoom("The Chat Window -- from opener");
}
public void actionPerformed(ActionEvent ae) {
if (!window.isVisible()) {
window.setVisible(true);
}
return;
}
does this sound about right? reason I'm asking is that Thomas Hawtin
said, "you don't even need to extend JFrame except in specialist cases.."
so I changed this..
public class ChatRoom extends JFrame implements ActionListener,
KeyListener {
to this..
public class ChatRoom implements ActionListener, KeyListener {
JFrame window = new JFrame();
but get errors (
(even though window opens fine now I need to make sure this works right
when it runs "for real" -- connecting to server, sending messages, etc..)
Thomas also says to open window from event-dispatch thread.. but I have
action to open window inside ActionPerformed() method.. where do I put
invokeListener() method (this is event-handling method?)
I'm trying to follow a simple model from this page,
http://java.sun.com/docs/books/tutorial/uiswing/learn/example1.html)
here invokeLater() is inside main method.. but Thomas Hawtin in same
post mentioned above said that a swing window that is not a JApplet
doesn't need an init() method NOR a main method.. (so, put
ActionPerformed() inside main method? help pls, am confused... threads
my main problem right now...) ok, lots of questions, I know.. thank
you very much...