Results 1 to 18 of 18
Like Tree1Likes
  • 1 Post By Tolls

Thread: Applet with swing componets

  1. #1
    MonkeyMan is offline Member
    Join Date
    Jan 2012
    Posts
    45
    Rep Power
    0

    Default Applet with swing componets

    I'm tying to get to the basics of applets but I'm running in to some seemingly very basic issues here.

    I made a new project normally.

    My project name is MyApplet.

    my package "myapplet" contains two files:

    first is a JAppletForm called myAppletForm. It contains one button and one combobox for testing purposes.

    It has this following code:

    Java Code:
    package main;
    
    /**
     *
     * @author Aleksi
     */
    public class myAppletForm extends javax.swing.JApplet {
    
        /** Initializes the applet myAppletForm */
        public void init() {
            try {
                java.awt.EventQueue.invokeAndWait(new Runnable() {
                    public void run() {
                        initComponents();
                    }
                });
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
            // TODO add your handling code here:
        }
    
    
        // Variables declaration - do not modify
        private javax.swing.JButton jButton1;
        private javax.swing.JComboBox jComboBox1;
        // End of variables declaration
    
    }
    The other file is my Main.java.

    That contains the following:

    Java Code:
    package main;
    
    import javax.swing.*;
    public class Main extends JApplet {
    
         private static void showGUI(){
    
            myAppletForm form = new myAppletForm();
            form.setSize(500, 450);
            form.setVisible(true);
    
        }
    
        public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable(){
    
            public void run(){
                showGUI();
            }
    
        });
    
        }
    
    }

    The form I'm trying to open through the main method does not appear. However, when I click run on the myAppletForm it opens normally. What am I missing?
    Last edited by MonkeyMan; 05-21-2012 at 07:42 PM.

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Applet with swing componets

    Both of your classes extend JApplet, is that on purpose? An Applet in an Applet?

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default Re: Applet with swing componets

    You can't run applets outside of a container (ie browser or somesuch).
    quad64bit likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: Applet with swing componets

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    MonkeyMan is offline Member
    Join Date
    Jan 2012
    Posts
    45
    Rep Power
    0

    Default Re: Applet with swing componets

    Quote Originally Posted by quad64bit View Post
    Both of your classes extend JApplet, is that on purpose? An Applet in an Applet?
    No it's not thanks.

  6. #6
    MonkeyMan is offline Member
    Join Date
    Jan 2012
    Posts
    45
    Rep Power
    0

    Default Re: Applet with swing componets

    Quote Originally Posted by Tolls View Post
    You can't run applets outside of a container (ie browser or somesuch).
    Alright how do I get this to run in a browser? Assuming this would then run in a browser like this?

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: Applet with swing componets

    Quote Originally Posted by MonkeyMan View Post
    Alright how do I get this to run in a browser? Assuming this would then run in a browser like this?
    The answer to that encompasses more than can fit in a forum thread. Go through the Lesson: Java Applets (The Java™ Tutorials > Deployment)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    MonkeyMan is offline Member
    Join Date
    Jan 2012
    Posts
    45
    Rep Power
    0

    Default Re: Applet with swing componets

    Quote Originally Posted by DarrylBurke View Post
    The answer to that encompasses more than can fit in a forum thread. Go through the Lesson: Java Applets (The Java™ Tutorials > Deployment)

    db
    Alright according to those instructions "Launching without JNLP" I made an html file like this:

    <html>
    <body>
    <applet code = "Main.class" archive = 'MyApplet.jar' width = "300" height = "300"/>
    </body>
    </html>

    to the build directory where all the classes are and pasted the MyApplet jar there as well. Running the html file turns error:


    Java Plug-in 1.6.0_31
    Using JRE version 1.6.0_31-b05 Java HotSpot(TM) Client VM
    User home directory = C:\Users\Aleksi
    ----------------------------------------------------
    c: clear console window
    f: finalize objects on finalization queue
    g: garbage collect
    h: display this help message
    l: dump classloader list
    m: print memory usage
    o: trigger logging
    q: hide console
    r: reload policy configuration
    s: dump system and deployment properties
    t: dump thread list
    v: dump thread stack
    x: clear classloader cache
    0-5: set trace level to <n>
    ----------------------------------------------------


    java.lang.NoClassDefFoundError: Main (wrong name: Main/Main)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(Unknown Source)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknow n Source)
    at sun.plugin2.applet.Applet2ClassLoader.findClass(Un known Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(U nknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Un known Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Un known Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unk nown Source)
    at sun.plugin2.applet.Plugin2Manager.createApplet(Unk nown Source)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionR unnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
    Exception: java.lang.NoClassDefFoundError: Main (wrong name: Main/Main)
    java.lang.NoClassDefFoundError: myAppletForm (wrong name: Main/myAppletForm)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(Unknown Source)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknow n Source)
    at sun.plugin2.applet.Applet2ClassLoader.findClass(Un known Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(U nknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Un known Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Un known Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unk nown Source)
    at sun.plugin2.applet.Plugin2Manager.createApplet(Unk nown Source)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionR unnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
    Exception: java.lang.NoClassDefFoundError: myAppletForm (wrong name: Main/myAppletForm)
    java.lang.NoClassDefFoundError: Main (wrong name: Main/Main)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(Unknown Source)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknow n Source)
    at sun.plugin2.applet.Applet2ClassLoader.findClass(Un known Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(U nknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Un known Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Un known Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unk nown Source)
    at sun.plugin2.applet.Plugin2Manager.createApplet(Unk nown Source)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionR unnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
    Exception: java.lang.NoClassDefFoundError: Main (wrong name: Main/Main)


    So there's still something very wrong here.

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

    Default Re: Applet with swing componets

    java.lang.NoClassDefFoundError: Main (wrong name: Main/Main)
    It looks like Your class is in the package: Main. The full classname is: Main.Main

    Why does the posted code have: package myapplet;
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    MonkeyMan is offline Member
    Join Date
    Jan 2012
    Posts
    45
    Rep Power
    0

    Default Re: Applet with swing componets

    Quote Originally Posted by Norm View Post
    It looks like Your class is in the package: Main. The full classname is: Main.Main

    Why does the posted code have: package myapplet;
    Ah sorry I refactored it at some point after posting the code. I'll fix that in the posted code.

    The correct package n class are indeed main and main.

    Previous question thusly still stands.

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

    Default Re: Applet with swing componets

    You need to change where you reference the classname to use the correct classname that includes the package.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    MonkeyMan is offline Member
    Join Date
    Jan 2012
    Posts
    45
    Rep Power
    0

    Default Re: Applet with swing componets

    Quote Originally Posted by Norm View Post
    You need to change where you reference the classname to use the correct classname that includes the package.
    I tried to change the html file to say:

    <html>
    <body>
    <applet code = "Main.Main" archive = "MyApplet.jar" width = "300" height = "300"/>
    </body>
    </html>

    Like the instructions I was given. It seperated package and class by dot like that. However, I receive the following:

    load: class Main.Main not found.
    java.lang.ClassNotFoundException: Main.Main
    at sun.plugin2.applet.Applet2ClassLoader.findClass(Un known Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(U nknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Un known Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Un known Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unk nown Source)
    at sun.plugin2.applet.Plugin2Manager.createApplet(Unk nown Source)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionR unnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
    Exception: java.lang.ClassNotFoundException: Main.Main

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

    Default Re: Applet with swing componets

    You're a little sloppy with the case of the names:

    package main;

    code = "Main.Main"

    The correct package n class are indeed main and main.
    Look in the jar file with a zip utility and see where the class files are located and how they are named.
    They must be in a folder with the name of the package.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    MonkeyMan is offline Member
    Join Date
    Jan 2012
    Posts
    45
    Rep Power
    0

    Default Re: Applet with swing componets

    Grr I refactored the package back to "myapplet" to avoid all the main confusion...

    The jar file now contains myapplet folder under which are classes Main and myAppletForm

    the path I'm using for the index.html is: C:\Users\Aleksi\Documents\NetBeansProjects\MyApple t\build\classes\myapplet

    That path now contains the html file, MyApplet.jar file, Main.class and myAppletForm.class.

    the html file now says:

    <html>
    <body>
    <applet code = myapplet.Main archive = MyApplet.jar width = 300 height = 300/>
    </body>
    </html>

    error I get now:

    java.lang.reflect.InvocationTargetException
    at com.sun.deploy.util.DeployAWTUtil.invokeAndWait(Un known Source)
    at sun.plugin2.applet.Plugin2Manager.runOnEDT(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager.createApplet(Unk nown Source)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionR unnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
    Caused by: java.lang.ClassCastException: myapplet.Main cannot be cast to java.applet.Applet
    at sun.plugin2.applet.Plugin2Manager$12.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
    Exception: java.lang.reflect.InvocationTargetException

  15. #15
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

    Default Re: Applet with swing componets

    ClassCastException: myapplet.Main cannot be cast to java.applet.Applet
    Does myapplet.Main extend the (J)Applet class?
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    MonkeyMan is offline Member
    Join Date
    Jan 2012
    Posts
    45
    Rep Power
    0

    Default Re: Applet with swing componets

    Quote Originally Posted by Norm View Post
    Does myapplet.Main extend the (J)Applet class?
    No, it doesn't. The extension is in the JAppletForm I'm creating a new out of in the Main class. To avoid the applet within an applet situation quad64 asked me of, removing the extensions from the JAppletForm obviously breaks the inherit JAppletForm initComponents method.

    But having both of em extend helps none. Still says:


    java.lang.reflect.InvocationTargetException
    at com.sun.deploy.util.DeployAWTUtil.invokeAndWait(Un known Source)
    at sun.plugin2.applet.Plugin2Manager.runOnEDT(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager.createApplet(Unk nown Source)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionR unnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
    Caused by: java.lang.ClassCastException: myapplet.Main cannot be cast to java.applet.Applet
    at sun.plugin2.applet.Plugin2Manager$12.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
    Exception: java.lang.reflect.InvocationTargetException

  17. #17
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

    Default Re: Applet with swing componets

    The code=class should extend the applet class
    If you don't understand my response, don't ignore it, ask a question.

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default Re: Applet with swing componets

    You need to decide what you are trying to run.
    If it's an applet then, as Norm says, the main class needs to extend Applet or JApplet.
    Those are the entry point classes for an applet (they don't use main()).

    If it's a desktop Swing application then you don't use (J)Applet, but will need to build your app via a JFrame.
    That will use a main() method as an entry point.

    At the moment you are munging the two things together and, not surprisingly, it's not working.

    You need to go through that tutorial that Darryl linked to above, where all this is explained.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. game componets
    By PRW56 in forum New To Java
    Replies: 20
    Last Post: 04-06-2012, 08:38 AM
  2. Replies: 3
    Last Post: 08-15-2011, 01:56 PM
  3. Swing applet problem
    By link6790 in forum New To Java
    Replies: 9
    Last Post: 05-24-2011, 02:31 PM
  4. Snowman Applet - SWING
    By Angry@Java in forum New To Java
    Replies: 1
    Last Post: 05-05-2011, 02:19 AM
  5. Change Applet to Swing
    By aryowap in forum AWT / Swing
    Replies: 4
    Last Post: 07-08-2010, 09:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •