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 / First Aid / December 2005

Tip: Looking for answers? Try searching our database.

Converting gui programm into Applet

Thread view: 
d.schulz81@gmx.net - 02 Dec 2005 18:24 GMT
HI there

currently we are trying to convert a java gui program into an applet.
now that we are inheriting from JApplet we have the following error
message:
(formerly the program inherited from JComponent)

Laden: Instanz von
freestyleLearningGroup.independent.util.FLGPlotter2D.class kann nicht
erstellt werden.
java.lang.InstantiationException:
freestyleLearningGroup.independent.util.FLGPlotter2D
    at java.lang.Class.newInstance0(Unknown Source)
    at java.lang.Class.newInstance(Unknown Source)
    at sun.applet.AppletPanel.createApplet(Unknown Source)
    at sun.applet.AppletPanel.runLoader(Unknown Source)
    at sun.applet.AppletPanel.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

Means the instance could not be created....

Is there a problem running threads in Applets?
What else could cause the failure?

Dennis
Thomas Hawtin - 02 Dec 2005 18:56 GMT
> currently we are trying to convert a java gui program into an applet.
> now that we are inheriting from JApplet we have the following error
[quoted text clipped - 14 lines]
>
> Means the instance could not be created....

Looking at the top of the stack trace, the exception is coming from
Class.newInstance. If we check the docs we see:

"InstantiationException - if this Class represents an abstract class, an
interface, an array class, a primitive type, or void; or if the class
has no nullary constructor; or if the instantiation fails for some other
reason."

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#newInstance()

So presumably FLGPlotter2D does not have a no-args constructor. Not a
very friendly error report.

> Is there a problem running threads in Applets?
> What else could cause the failure?

There are the usual Swing threading issues. For Swing you should extend
javax.swing.JApplet instead of java.applet.Applet directly, as you say.
Apparently for historical reasons, some applet methods are called
outside of the Event Dispatch Thread (EDT). Therefore you need a
fantastical bit of boilerplate before accessing Swing components (or AWT
components if you want to be safe).

    @Override
    public void init() {
        try {
            EventQueue.invokeAndWait(new Runnable() {
                    public void run() {
                        // ...fiddle with components here...
                    }
            });
        } catch (java.lang.InterruptedException exc) {
            // I assume you are not using Thread.interrupt.
            // May come from applet shutdown sequence.
            // Just swallow interrupted state and exit init.
            // ... or your error handling here ...
        } catch (java.lang.reflect.InvocationTargetException exc) {
            // Must be an unchecked exception - throw unwrapped.
            if (exc instanceof RuntimeException) {
                throw (RuntimeException)exc;
            } else if (exc instanceof Error) {
                throw (Error)exc;
            } else {
                throw new Error(
                    "...your comment about this being impossible...",
                    exc.getCause()
                );
            }
            // ... or your error handling here ...
        }
    }

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/



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.