Results 1 to 3 of 3
  1. #1
    Daniel is offline Member
    Join Date
    Jun 2007
    Posts
    91
    Rep Power
    0

    Default Create a Applet in the page HTML

    It was about time to start with this things but i don't even know how to create any applet that will run on my site even saying "hello" in JOptionPane box when you press button... Can you refer me to sites or give some starting tips on how to create my first JApplet and to embeed it in my site?

    P.S.: I tried something but it doesnt load : Untitled Document

    Thanks

    Daniel:o

  2. #2
    Albert is offline Senior Member
    Join Date
    Jun 2007
    Posts
    114
    Rep Power
    0

    Default

    Have you had a look at Sun Tutorials on Applets. Very good starting point if you have just starting programming in Java.

    Here is an example of a very very basic applet, that will demonstrate the various methods and when try are called once you have opened the page.

    Please save in a file MyApplet.java

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    // simple applet
    public class MyApplet extends JApplet {
        private String name;
        /*
          * Constructor for MyApplet extends JApplet
          */
        public MyApplet(){
        } //-- ends class constructor
        
        public void init() {
            this.name = new String();
            this.name = JOptionPane.showInputDialog( null, "What is your name? " );
            System.out.println("+++++++++++++ Applet INITIALIZED +++++++++++++");
            System.out.println("You said that your name was: " + this.name );
        } //-- ends instance method init
        
        // runs when you pages opens or when you refresh page
        public void start() {
            System.out.println("*************** Applet is START ***************");
        } //-- ends instnce method start
        
        // runs every time your referesh page...or when you try and 
        // and resizes browser
        public void paint( Graphics g ) {
            System.out.println("--------------- Applet is PAINTING ---------------");
            g.drawString( "You said your name was: " + this.name, 10, 10 );
        } //-- ends instance method paint
        
        // runs when user minimizes browser or when navigates away from page
        public void stop() {
            System.out.println(" _________ Applet STOPPED _______");
        } //-- ends instance method stop
        
        // runs when applet is destroyed...or when user navigates to another page
        public void destroy() {
            System.out.println("%%%%%%%%%%%% Applet DESTORYED %%%%%%%%%%%%");
        } //-- ends instance method destroy
        
    } //-- ends class definition
    Once you have done that, then please create HTML page...and dump the following contents inside it. I have called it MyApplet.html
    Java Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"/>
    <title>My Applet</title>
    </head>
    <body>
    <h1>My Applet</h1>
    <applet code="MyApplet.class" CODEBASE="" width=500 height=500>
    	<p>Sorry, but you need a java enabled browser in order to view the applet.</p>
    </applet>
    </body>
    </html>
    Instead of viewing the applet in a web browser, please view it using the appletviewer command...so that you can see how all the methods are run...etc

    Of course, you should compile the applet first.
    Java Code:
    javac *.java
    and then run the appletviewer by executing the following command
    Java Code:
    appletviewer MyApplet.html
    You should see from the command console..the sequence of all the methods and how they are executed. play around with the appletviewer...by minimizing, maximinzing, etc...just to see how all the methods are fired...and in what sequence...etc

    Greetings
    Albert:rolleyes:

  3. #3
    Daniel is offline Member
    Join Date
    Jun 2007
    Posts
    91
    Rep Power
    0

    Default

    Thanks much! It works and i got (at least i think so) the point...

    But... What about implementing chat in java and putting it as applet on my site so i'll have java chat?

    I have one chat in java but i don't know if possible to embeed it on my site as applet?

    Thanks!

    Daniel:o

Similar Threads

  1. html web page parsing/scraping
    By orchid in forum Advanced Java
    Replies: 3
    Last Post: 10-21-2010, 01:34 PM
  2. Replies: 0
    Last Post: 03-10-2008, 02:36 PM
  3. How to view applet from html page.
    By jwzumwalt in forum Java Applets
    Replies: 2
    Last Post: 11-24-2007, 04:21 AM
  4. HTML page
    By bbq in forum New To Java
    Replies: 1
    Last Post: 07-05-2007, 03:46 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
  •