Results 1 to 13 of 13
  1. #1
    anarelle's Avatar
    anarelle is offline Member
    Join Date
    Mar 2011
    Location
    Argentina
    Posts
    21
    Rep Power
    0

    Default new to Java applets... where to start??

    I'm pretty new to Java overall, but I've built a JFrame app before, so I kinda know the basics to it.
    However, I've read a few Applet tutorials and caught a few tips and info from all of them. For example, I've seen I don't need a "main" method but an "init" and "run" methods (don't know if I need both, though). What I don't know is exactly how to implement them.
    I've been reading http://book.javanb.com/jfc-swing-tut...7lev1sec1.html but this talks about threads... Do I absolutely need threads?

    Anyway, what I'm looking for is some resource that'll teach me how to build Swing Applets (most of the stuff I find out there is about awt applets) from scratch, since my attempts at building one aren't working.

    Thanks!

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,605
    Rep Power
    23

    Default

    No you do not need to use Threads.
    Go to this site:The Really Big Index
    and Find Applets for lots about writing applets from the Java group.

  3. #3
    anarelle's Avatar
    anarelle is offline Member
    Join Date
    Mar 2011
    Location
    Argentina
    Posts
    21
    Rep Power
    0

    Default

    Thanks Norm!
    I managed to start building my applet. But when I try to include it in a html file (I'm reading Deploying an Applet (The Java™ Tutorials > Deployment > Java Applets)) I just can't seem to make it simple and clean. It says something about compiling my applet into a jar file and then building a jnlp file that needs a main-class attribute to load the class containing the "main" method... but as far as I've seen, applets don't have a main method but an init method...
    So basically, how do I use my applet? :P
    Thanks again!

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,605
    Rep Power
    23

    Default

    For a first version of your applet, you won't need a jar file or any of the jnlp stuff. That is for deployment after you get it working and want to share it.

    Put all the applet's class files, hopefully there are only a few, in a test folder.
    Create an HTML file with an applet tag:
    <APPLET code="THE CLASS NAME HERE" width=500 height=500>
    </APPLET>
    and open the html file in a browser.
    Set the width and height for what your applet needs.

  5. #5
    anarelle's Avatar
    anarelle is offline Member
    Join Date
    Mar 2011
    Location
    Argentina
    Posts
    21
    Rep Power
    0

    Default

    Thanks again :)
    The reason I was trying the jar file thing is that I had tried the <applet> tag you mentioned but I wasn't successful.
    My applet runs normally when I press "run" in Eclipse, but it doesn't seem to work in my html file (I see a white empty rectangle where the applet should be showing).
    I have 2 classes, one of which has the init method and instantiates the other class, which contains all the GUI components. I built both .class files and then included the one that contains the init method in my <applet> tag, like this:
    <html>
    <body bgcolor=#000000>
    <APPLET code="Inicio.class" width=750 height=400></APPLET>
    </body>
    </html>

    I tried without the ".class" bit just in case, but still doesn't work. What am I doing wrong?

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,605
    Rep Power
    23

    Default

    What am I doing wrong?
    Look at the browser's java console for error messages.
    Leave off the .class

    Are the html and class files in the testing folder?

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

    Default

    Is there a package statement at the top of the class that extends JApplet?

    db

  8. #8
    anarelle's Avatar
    anarelle is offline Member
    Join Date
    Mar 2011
    Location
    Argentina
    Posts
    21
    Rep Power
    0

    Default

    The java console gives the following information:

    Exception in thread "thread applet-Inicio.class-3" java.lang.NoClassDefFoundError: Inicio$1
    at Inicio.init(Inicio.java:15)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionR unnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)


    This is what my main class has (class name is "Inicio"):

    import javax.swing.SwingUtilities;
    import javax.swing.JApplet;

    public class Inicio extends JApplet{

    private static final long serialVersionUID = 1L;

    public void init() {
    try {
    SwingUtilities.invokeAndWait(new Runnable() {
    public void run() {
    }
    });
    setSize(750, 400);
    createGUI();
    } catch (Exception e) {
    System.err.println("createGUI didn't complete successfully");
    }
    }

    private void createGUI() {
    //Create and set up the content pane.
    Ventana panel = new Ventana();
    panel.setOpaque(true);
    setContentPane(panel);
    }

    }


    And the other class extends from JPanel and has a few components that show normally when I run my applet within Eclipse.

  9. #9
    anarelle's Avatar
    anarelle is offline Member
    Join Date
    Mar 2011
    Location
    Argentina
    Posts
    21
    Rep Power
    0

    Default

    (edit - I got an error saying my message hadn't been posted, so I reposted it and found out it was here twice :P)

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,605
    Rep Power
    23

    Default

    ava.lang.NoClassDefFoundError: Inicio$1
    Did you copy ALL of the .class files to your test folder?
    The $1 on the end of the class name: Inicio$1
    is for an inner anonymous class, probably the new Runnable() {


    Can you explain the steps you took for this test?

  11. #11
    anarelle's Avatar
    anarelle is offline Member
    Join Date
    Mar 2011
    Location
    Argentina
    Posts
    21
    Rep Power
    0

    Default

    Oh... I have the test.html, Ventana.class and Inicio.class files in my folder, but not the Runnable.class. In fact, I just copied that bit from the tutorial, but I didn't quite get why I have to use SwingUtilities.invokeAndWait(new Runnable() {
    public void run() {
    }
    I just open the html file, but it seems I need some more classes in the folder where the html file lives...?

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,605
    Rep Power
    23

    Default

    You need ALL of the class files that are created when you compile your source program.

  13. #13
    anarelle's Avatar
    anarelle is offline Member
    Join Date
    Mar 2011
    Location
    Argentina
    Posts
    21
    Rep Power
    0

    Default

    Apparently, there was an Inicio$1.class file I hadn't copied into my folder. I added that one and it worked :)
    Thanks a lot!!

Similar Threads

  1. Replies: 1
    Last Post: 01-18-2011, 06:27 PM
  2. All applets fail to start
    By dathoedezt in forum Java Applets
    Replies: 4
    Last Post: 01-13-2011, 11:25 PM
  3. Java Applets
    By kevinnrobert in forum Java Applets
    Replies: 1
    Last Post: 04-02-2010, 01:16 PM
  4. Replies: 5
    Last Post: 03-20-2009, 10:04 PM
  5. Applets (init, start, stop, destroy)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-12-2007, 10:57 AM

Posting Permissions

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