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 / November 2005

Tip: Looking for answers? Try searching our database.

applet in a frame in the browser window

Thread view: 
giangiammy@gmail.com - 07 Nov 2005 13:49 GMT
Hi all,

my application is an applet which I want run both as applet and
standalone program, defined as

public class Jnoti extends Applet {
   InterfaceLayout interfaceLayout = new InterfaceLayout(6000);

   public static void main(String[] args)
   {
    Jnoti applet = new Jnoti();
    applet.init();
    applet.start();
   }
}

the InterfaceLayout class is the one which creates the frame
and its contents.

It's everything Ok when started standalone, while when started
as an applet via browser, it creates an empty frame in the browser
window, and a new separated windows with my contents.

How can I get that my contents be included in the browser
window with no separated windows??

thanks
giammy

--
Gianluca Moro          http://groups.yahoo.com/group/embeddeditalia/
ISCRIVITI alla         Mailing List Italiana su LINUX EMBEDDED
giangiammy@yahoo.com   Visit http://ilpinguino.altervista.org/
Roedy Green - 07 Nov 2005 14:02 GMT
On 7 Nov 2005 05:49:18 -0800, "giangiammy@gmail.com"
<giangiammy@gmail.com> wrote, quoted or indirectly quoted someone who
said :

>my application is an applet which I want run both as applet and
>standalone program, defined as
see convert your program to a hybrid see the code at
http://mindprod.com/jgloss/applet.html
or
http://mindprod.com/jgloss/japplet.html

The other way around the problem is to use JAWS to launch your applet
without a browser.
see http://mindprod.com/jgloss/javawebstart.html

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Andrew Thompson - 07 Nov 2005 14:07 GMT
> my application is an applet which I want run both as applet and
> standalone program, defined as
>
> public class Jnoti extends Applet {
>     InterfaceLayout interfaceLayout = new InterfaceLayout(6000);

This will call and instantiate your applet/application.
If it calls 'setVisible(true)' from within the constructor,
it will appear on-screen.

>     public static void main(String[] args)

..but you do not need a separate main() (or any
call to main, for that matter) to get the applet
into the web page.  You simply call the applet
as you would call any standard applet.

> How can I get that my contents be included in the browser
> window with no separated windows??

Dump the separate class with the second main and call
the applet class directly.

Failing that, put a link to your web page, and link
to the code.  Without seeing the code of the
InterfaceLayout class, it is hard to say more exactly.

As an aside, applets are not for noobs, if you
are trying this as 'a simple demonstration', I
recommend you try any number of simpler things first.
<http://www.physci.org/codes/javafaq.jsp#appfirst>

HTH
giangiammy@gmail.com - 07 Nov 2005 15:25 GMT
the code is the following:
when I call it with
# java Jnoti
I get 1 window with the message:
I'd like to have the same result calling
# appletviewer Jnoti.html
which on the contrary opens 2 windows!

My idea is to have the main class Jnoti interface independent,
while keeping all the interface code in InterfaceLayout.

I think I should say to InterfaceLayout to use the frame opened
by the browser - probably I'm missing this step :-(

thanks
giammy

InterfaceLayout.java:
import java.awt.*;

class InterfaceLayout extends Frame {

   public InterfaceLayout(int typ) {

    Canvas c;
    Frame f = this;

    c = new Canvas() {

        public void paint(Graphics g) {
           // super.paint(g); CREA IL FLICKER
           g.drawString("Hello", 10, 10);
        }
       };

    c.setSize(300, 300);

    f.add(c);
    f.pack();
    f.show();
   }
}

Jnoti.java:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.applet.*;

/**
* The main class: will create interface, event managers,
* and dispatch the events
*/
public class Jnoti extends Applet {

   InterfaceLayout interfaceLayout;

   public Jnoti() {
    interfaceLayout = new InterfaceLayout(6000);
   }

   /*
    * for testing from command line
    */
   public static void main(String[] args)
   {
    Jnoti applet = new Jnoti();
    applet.init();
    applet.start();
   }

}

Jnoti.html:
<!doctype html public "-//w3c//dtd html 3.2//en">
<html>
<head>
<title>Applet Socket Connection Test</title>
</head>

<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080"
alink="#ff0000">

<center>
<h1>Jnoti</h1>
</center>

<applet width="800" height="600" code="Jnoti.class"
codebase="."></applet>

</body>
</html>
Andrew Thompson - 07 Nov 2005 16:17 GMT
> the code is the following:

<sscce>
import java.awt.*;
import java.applet.*;

/**
 * The main class: will create interface, event managers,
 * and dispatch the events
 */
public class Jnoti extends Applet {

  /** Applets do not usually need a constructor, the
  work is done in 'init()'. */
  public void init() {
    Canvas c;
    c = new Canvas() {

      public void paint(Graphics g) {
      // super.paint(g); CREA IL FLICKER
      g.drawString("Hello", 10, 10);
      }
    };
    c.setSize(300, 300);
    // add the canvas to the applet
    add(c);
  }

  /*
   * for testing from command line
   */
  public static void main(String[] args)
  {
    Jnoti applet = new Jnoti();
    applet.init();
    applet.start();

    Frame f = new Frame("Jnoti applet");

    //add the applet to the frame.
    f.add(applet);
    f.pack();
    f.setVisible(true);
  }
}
</sscce>

> Jnoti.html:
>  <!doctype html public "-//w3c//dtd html 3.2//en">
> <html>
....
> <applet width="800" height="600" code="Jnoti.class"

> <applet width="800" height="600" code="Jnoti"

> codebase="."></applet>

..should do it.

HTH
Roedy Green - 07 Nov 2005 16:40 GMT
On 7 Nov 2005 07:25:38 -0800, "giangiammy@gmail.com"
<giangiammy@gmail.com> wrote, quoted or indirectly quoted someone who
said :

>My idea is to have the main class Jnoti interface independent,
>while keeping all the interface code in InterfaceLayout.

I gave you a technique that will work.  
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

zero - 07 Nov 2005 20:50 GMT
> Hi all,
>
[quoted text clipped - 24 lines]
> thanks
> giammy

Ok, what you're doing is creating a new InterfaceLayout.  But,
InterfaceLayout is a Frame, and a Frame is a window.  So, you're creating
a window *on top of* the browser window in which the applet is running.  
What you should do is create the GUI in the Applet (or in a Panel, but
not a Frame).  Then in the main method, create a Frame and add the Applet
to it.

public class Jnoti extends Applet
{
  public void init()
  {
     // create the GUI
  }

  public static void main(String[] args)
  {
     Frame application = new Frame();
     Applet applet = new Jnoti();
     application.getContentPane().add(applet);
     application.setSize(300, 300);
     application.setVisible(true);
     applet.init();
     applet.start();
  }
}

This will work, but it's not exactly like it should be (among other
things, the applet is never stopped).  For a more correct example look at
Roedy's site
http://mindprod.com/jgloss/applet.html
under the heading Switch Hitter.

Note: when creating a hybrid applet/application always design it as
applet first.  There is a lot that will be possible in applications, but
not in applets.


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.