Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2007

Tip: Looking for answers? Try searching our database.

How to change Swing app to JApplet

Thread view: 
JTL.zheng - 21 Jun 2007 16:43 GMT
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
Tom Hawtin - 21 Jun 2007 18:35 GMT
>   public class Applet extends JApplet {
>
[quoted text clipped - 10 lines]
> 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
bencoe@gmail.com - 21 Jun 2007 20:09 GMT
> >   public class Applet extends JApplet {
>
[quoted text clipped - 44 lines]
>
> 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.
Andrew Thompson - 22 Jun 2007 09:55 GMT
>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.

Signature

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

JTL.zheng - 22 Jun 2007 10:59 GMT
> 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.
Andrew Thompson - 22 Jun 2007 11:54 GMT
>> 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:

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..
<http://www.physci.org/codes/javafaq.html#exact>

>http://hartech.cn/jbf.rar

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.

Signature

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

Andrew Thompson - 22 Jun 2007 11:58 GMT
>>> 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:
>
>Note how I did not ask for your entire code.

Though I see how you might have thought that,
from the 1st paragraph I wrote.  

But to clarify.  No, if it is too big to post here
(about 300-400 lines), it is too big for me to
be downloading and 'fixing'.

Signature

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

Roedy Green - 22 Jun 2007 11:18 GMT
>how to change a Swing app to a JApplet?
>what should I change in codes?

see http://mindprod.com/jgloss/japplet.html#CONVERTING
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
JTL.zheng - 22 Jun 2007 12:48 GMT
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....
Andrew Thompson - 22 Jun 2007 13:13 GMT
...
>...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.

Signature

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

Roedy Green - 23 Jun 2007 10:46 GMT
>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.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
JTL.zheng - 23 Jun 2007 15:03 GMT
> 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?
Roedy Green - 23 Jun 2007 15:29 GMT
>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?

You should.  You should ask your threads to gracefully exit.

See http://mindprod.com/jgloss/thread.html#DEATHWATCH
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
JTL.zheng - 23 Jun 2007 17:56 GMT
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()
Tom Hawtin - 23 Jun 2007 19:42 GMT
> 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
JTL.zheng - 24 Jun 2007 03:34 GMT
> 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.
Andrew Thompson - 24 Jun 2007 05:37 GMT
>> 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

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.

>> 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.

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.

Signature

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

JTL.zheng - 24 Jun 2007 08:28 GMT
> 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?
Andrew Thompson - 24 Jun 2007 09:17 GMT
...
>It seems that JRE allocates only one VM to a firefox, although it has
>mult-tabs, mult-applets.
[quoted text clipped - 4 lines]
>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.   ;-)

Signature

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

JTL.zheng - 24 Jun 2007 16:27 GMT
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.
Roedy Green - 29 Jun 2007 14:14 GMT
>how to change a Swing app to a JApplet?
>what should I change in codes?

It is a lot easier to convert a JApplet to a application or a hybrid
than the reverse.  Have a look at some of my hybrids at
http://mindprod.com/products1.html

You move all your code to allocate and set up components to init.

Init also must set up the listeners and do the layout. I use IntelliJ
to refactor the code to put each of these functions in its own method
called by init.

You then need to write your start,  stop and destroy, often empty.

Make user you use JApplet.getContentpane rather than using JApplet
directly for setBackground, setLayout and add.

look at one of my programs like
http://mindprod.com/products1.html#AMERICANTAX
to see the overall structure.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.