applet in a frame in the browser window

G

giangiammy

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
 
A

Andrew Thompson

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
 
G

giangiammy

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>
 
A

Andrew Thompson

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);
}
}
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
 
R

Roedy Green

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.
 
Z

zero

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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top