Reputation: 1073
I have made a Loader Applet which greets the user and when user clicks the button displayed on that Applet it then launches the main applet and the Loader Applet is destroyed.
But on clicking Another applet is not launched !
Loader Applet:
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
public class Loader extends JApplet implements ActionListener{
Display secondApplet;
Button button;
@Override
public void init() {
setSize(800,600);
}
@Override
public void start() {
setLayout(new FlowLayout());
button = new Button ("Click me !!");
add(button);
button.addActionListener(this);
}
@Override
public void paint(Graphics g) {
}
@Override
public void actionPerformed(ActionEvent e) {
secondApplet = (Display)getAppletContext().getApplet("Display");
if (secondApplet != null) {
secondApplet.init();
secondApplet.start();
}
else {
System.out.println("Not Running\n");
}
}
}
Display Applet:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JApplet;
public class Display extends JApplet {
@Override
public void init() {
setSize(600,400);
}
@Override
public void paint(Graphics g) {
g.fillRect(0, 0, this.getWidth(), this.getHeight());
}
}
How can I create an instance of the other Applet and destroy the current Applet !!
Upvotes: 1
Views: 12598
Reputation: 168845
There are so many things wrong with the applets it is hard to know where to begin. But let us concentrate 1st on a more sensible strategy to cause the change beteween one view and another.
CardLayout
getAppletContext().showDocument(secondAppletURL);
. Presumably secondAppletURL
is different to the URL of the page that hosts the first applet.OK - the problems with the first applet:
start()
method should be moved to the init()
method, since the start()
method might be called repeatedly. Then there is no reason to override start()
at all.JApplet
) & AWT (e.g. Button
) components without good cause. In this case, use JButton
instead of Button
.ActionListener
than implement it on the parent class.paint()
with an empty implementation is not a good idea. The original paint()
draws the applet and components, so now it would do nothing.actionPerformed()
are equally nonsensical. An applet does not get included into the AppletContext
until after init()
& start()
have been called, which would mean that calling those methods explicitly causes them to be invoked a second time. While the start()
method is meant to be called multiple times, the init()
method should only be called once.2nd Applet.
paint()
method ..would paint the BG color (or the FG color - not sure) but nothing else. Again, don't override it.Upvotes: 2
Reputation: 1586
Since an Applet/JApple is a java.awt.Panel itself, then you can embed one into the other, for your specific case you can embed Display into Loader using a Panel in Loader to reload Display as you need.
Something like this:
Panel container = new Panel();
container.setLayout(new GridLayout(1,0));
container.add(secondApplet); //Display Applet
add(container):
secondApplet.init();
secondApplet.start();
button.setVisible(false);
Upvotes: 3
Reputation: 45090
Try this method to load another applet. See if it works.
Class applet2 = Class.forName("Applet2");
Applet appletToLoad = (Applet)applet2.newInstance();
appletToLoad.setStub(this);
setLayout(new GridLayout(1,0));
add(appletToLoad);
appletToLoad.init();
appletToLoad.start();
Upvotes: 1