How to change Swing app to JApplet

J

JTL.zheng

how to change a Swing app to a JApplet?
what should I change in codes?

in my swing app my entrance is like:
public static void main(String[] args) {

// call in EDT
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new UI();
}
});
}

and I write my JApplet like this:
public class Applet extends JApplet {

private static final long serialVersionUID = -3683798728718521374L;
public void init() {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new UI();
}
});
}
}

but it doesn't work....
what code should I change?

Thank you very much in advance
 
T

Tom Hawtin

JTL.zheng said:
public class Applet extends JApplet {

private static final long serialVersionUID = -3683798728718521374L;
public void init() {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new UI();
}
});
}
}

but it doesn't work....
what code should I change?

Technically you should set the GUI up before returning from init:

public class SomeApplet extends JApplet {
private static final long serialVersionUID = -3683798728718521374L;
@Override
public void init() {
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
new UI();
}
});
} catch (InterruptedException exc) {
Thread.currentThread().interrupt();
} catch (java.lang.reflect.InvocationTargetException exc) {
Throwable cause = exc.getCause();
if (cause instanceof RuntimeException) {
throw (RuntimeException)cause;
} else if (cause instanceof Error) {
throw (Error)cause;
} else {
throw new Error(cause);
}
}
}
}

(Disclaimer: Not tested or even compiled.)

However, I don't know whether that actually makes any difference.

Tom Hawtin
 
B

bencoe

Technically you should set the GUI up before returning from init:

public class SomeApplet extends JApplet {
private static final long serialVersionUID = -3683798728718521374L;
@Override
public void init() {
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
new UI();
}
});
} catch (InterruptedException exc) {
Thread.currentThread().interrupt();
} catch (java.lang.reflect.InvocationTargetException exc) {
Throwable cause = exc.getCause();
if (cause instanceof RuntimeException) {
throw (RuntimeException)cause;
} else if (cause instanceof Error) {
throw (Error)cause;
} else {
throw new Error(cause);
}
}
}

}

(Disclaimer: Not tested or even compiled.)

However, I don't know whether that actually makes any difference.

Tom Hawtin

I'm assuming your new UI() method makes an instance of a Swing
container at some point? If this is the case, keep in mind the JApplet
now constitutes the main frame of your Swing application... You should
be able to keep stuff pretty much the same but at some point you'll
want to add your UI as a component to the JApplet.

Ben.
 
A

Andrew Thompson

JTL.zheng said:
how to change a Swing app to a JApplet?

There might be a number of changes to make,
depending on the code you did not show.
what should I change in codes?

<dws>
Line 54, change the '.', to a ',' and it should be
set to go.
</dws>

Failing that, give more details*.
but it doesn't work....

Lazy, is it?

* If not, try actually *describing* what went wrong,
what you saw but did not expect to see, or vice
versa, what you did not see, but expected to see.
Were there any errors listed in the Java console?
what code should I change?

Let me know at least the details of what is going
wrong, and I might be able to help further.

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200706/1
 
J

JTL.zheng

Let me know at least the details of what is going
wrong, and I might be able to help further.

It's too much to print here, you can download my whole project in:
http://hartech.cn/jbf.rar


inside it:

JBallField.jar
is the swing app which I want to change to a Applet

JBallField Specific.doc
is the document, but it's written in chinese

_JP/src/hartech/kids/jballfield
is all this project's source files

could you tell me what should be changed or send me a changed package

Thank you very much in advance.
 
A

Andrew Thompson

JTL.zheng said:
It's too much to print here, you can download my whole project in:

Note how I did not ask for your entire code.
If posting code, I suggest the simplest example
that fails for you. An SSCCE.

However, I do not think we necessarily need an
SSCCE to solve this. Which is why I asked you
to tell me "what is going wrong". Another way to
put that is..

What am I supposed to do with a rar file, anyway?

By running the Jar tool, it is possible to create a
ZIP (or JAr) file that can be opened by anybody
with the SDK, or Windows..

...
could you tell me what should be changed or send me a changed package

What is your budget? How many dollars are you
prepared to pay for my consultancy services?

Because this is a discussion forum, whereas you
are asking for more specific (and private) help.

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200706/1
 
J

JTL.zheng

that's all right, Thank you all the same

It's just hard to describe.
It doesn't throw any Exceptions.

when I open it first in a web browser(firefox)
it works as expect.

but when I reopen it in the same web browse
It acts like it keep the old variable values of the first time I run
it
I have multi-threads in it.
will it keep the thread running after I close the firefox tag which
show the applet?
bizarre....
 
A

Andrew Thompson

JTL.zheng wrote:
...
...when I reopen it in the same web browse
It acts like it keep the old variable values of the first time I run
it
I have multi-threads in it.

An applet has stop() and destroy() methods.
Some brtowsers even call those methods when the
end user leaves the page, or closes the browser.

The Threads should be stopped from within the
destroy() method.
will it keep the thread running after I close the firefox tag which
show the applet?

Yes, no, maybe. It depends on the browser/VM
combination.
bizarre....

No. People being utterly confused by applet
programming is quite common. Applets are
harder to develop, and get working reliably,
than applications.
 
R

Roedy Green

but when I reopen it in the same web browse
It acts like it keep the old variable values of the first time I run
it
I have multi-threads in it.

Sounds like your stop and/or destroy method does not gracefully
shutdown the threads.
 
J

JTL.zheng

Sounds like your stop and/or destroy method does not gracefully
shutdown the threads.
--

no,I did nothing in the destory()
besides thread, what else will it keep when I close a firefox tag
which showing the applet.
the objects? the class static variables?
 
J

JTL.zheng

Thank you very much

Is there a simple code just to clean up all the object variables,
class static variables and threads I created in applet?
so I can use it in destory()
 
T

Tom Hawtin

JTL.zheng said:
Is there a simple code just to clean up all the object variables,
class static variables and threads I created in applet?
so I can use it in destory()

You should avoid using variable statics for many reasons (even if you
call them singletons). If you have two browser windows open on the page
with the applet, both applets may share statics.

For threads, generally you want to start them in start and let them exit
shortly after stop. Let them exit nicely rather than trying to stop them
abruptly.

Tom Hawtin
 
J

JTL.zheng

If you have two browser windows open on the page
with the applet, both applets may share statics.

I have tried. It does. in defferent tags in firefox.
but it doesn't make sense....It should not share statics between
different applet, should it? is it useful?
in swing apps they don't share statics between different swing apps.
You should avoid using variable statics for many reasons (even if you
call them singletons).

but if avoiding using variable statics,it's difficult to communicate
between objects.
 
A

Andrew Thompson

JTL.zheng said:
I have tried. It does. in defferent tags

tags or tabs? The first implies to me, separate
applets (tags/elements) on the same web page,
the second indicates two web pages open in
different browser tabs.
...in firefox.
but it doesn't make sense....It should not share statics between
different applet, should it? is it useful?

As I tried to explain earlier, whether a browser
provides a separate VM for each applet, HTML
frame, HTML page or browser tab /instance, is
entirely up to the browser manufacturer.

If the applets are in the same VM - they will
share a single static member. If they are in
separate VM's, there will be more than one
static member (the static members will be
distinct to each applet).

The developer cannot *rely* on any particular
behaviour. One of the biggest mistakes one
can make when developing applets, is to
assume that *any* other browser acts the
same as the one you test in.
but if avoiding using variable statics,it's difficult to communicate
between objects.

I should not be too difficult. There are a
number of ways to get information from
one class to another.

How best to do it, could be determined
by the nature of the data, and how and
when it changes.

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200706/1
 
J

JTL.zheng

tags or tabs? The first implies to me, separate
applets (tags/elements) on the same web page,
the second indicates two web pages open in
different browser tabs.

it's tab...I mistake it...

Thank you very much.
I am more clear now.

It seems that JRE allocates only one VM to a firefox, although it has
mult-tabs, mult-applets.

and in the same VM different applets share the same statics.
all the classes in different applets are in the same name-space.

Although I close the tab which showing the applet,its static variables
will still remain.and those threads that doesn't be terminated in
destory() will still keep running.

is it right?
 
A

Andrew Thompson

JTL.zheng wrote:
...
It seems that JRE allocates only one VM to a firefox, although it has
mult-tabs, mult-applets.

and in the same VM different applets share the same statics.
all the classes in different applets are in the same name-space.

Although I close the tab which showing the applet,its static variables
will still remain....

So long as the browser keeps the VM running,
but..
...and those threads that doesn't be terminated in
destory() will still keep running.

..the Threads may be why the VM is left running.

In other words - stop the threads, and the 'static'
problem *might* be fixed for if the user leaves the
page, and later returns to it.

But it is best to fix the problem with statics
anyway, just in case the end user has two
copies of the same page (with applet that
uses static attributes) open.
is it right?

You seem to be getting there, but there are
still a lot of little details I have not bothered
to speculate on, or dared to mention. ;-)

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200706/1
 
J

JTL.zheng

Thank you very much.
I get it more clear now.
and I should avoid to use statics in applet.

do you always not use one static variable in applet?
and what about in a swing application?
or do you just not use any static in java?
I don't know anything about that, it seems that statics are useful.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top