Hi guys,
What is the difference in java code between opening an applet in a seperate window and loading it into the html page?
Printable View
Hi guys,
What is the difference in java code between opening an applet in a seperate window and loading it into the html page?
Can you give us more information? What do you mean by opening in a "separate window"? as in using a JDialog?
I mean such as in this example:
2-D Vector Field Simulation
I don't know which part of the java code is responsible for that behaviour. It certainly isn't html, I've checked.
It has the source code on that site and in it you'll see that they're creating one or several Frame objects which are likely what is being displayed. I recommend using Swing instead of AWT meaning using JApplets and perhaps JDialogs to show separate stand-alone windows.
Or an application (not an Applet) launched via WebStart.
Lesson: Java Web Start (The Java™ Tutorials > Deployment)
db
Hopefully this reply is useful to someone. I realize that the discussion began last summer, but I actually have a good answer to this question, and I have benefited so often from forums like these in the past, I wanted to give something back.
I've just implemented this option with my applet. That is, the applet can be either opened as usual to appear in the browser webpage, or it can be opened to display in a separate JFrame.
I have implemented it using three separate classes. The first I call the Launcher and it resides in the webpage as a normal JApplet. I also place a button on it that will bring up the applet in the JFrame when it loses the focus.
The second is a JApplet that extends the applet that I want to display in the separate frame. I call is the appletBox.
Its job is to override the basic applet commands that are used in my JApplet and relay them to the Launcher applet (called myBaseApplet in the appletBox). It also creates the JFrame that displays the applet. This part could have been done in the Launcher applet, but the overriding applet i/o calls would have been difficult. See the code for this also below.
Code for the Launcher applet is here:
Here is the code for appletBox:Code:public class Launcher extends JApplet {
appletBox box;
public void init() {
//Execute a job on the event-dispatching thread:
//creating this applet's GUI.
try {
javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
createGUI();
}
});
} catch (Exception e) {
System.err.println("createGUI didn't successfully complete");
System.out.println("\r\nError Log:"+e.toString());
System.out.println("\r\n");
e.printStackTrace();
}
}
public void createGUI() {
JButton button = new JButton("Click to use the Applet!");
button.setFont(new Font("Serif", Font.BOLD, 20));
Container cp = getContentPane();
cp.setLayout(new GridLayout(3, 1));
cp.add(new JPanel());
JPanel jp = new JPanel(new FlowLayout());
jp.add(button);
cp.add(jp);
button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
box.show();
}
});
box = new appletBox(this);
}
}
Let me know if this is useful!Code:public class appletBox extends myApplet {
JApplet myBaseApplet;
JFrame window;
/** Creates a new instance of appletBox */
public appletBox(JApplet applet) {
myBaseApplet = applet;
createGUI();
}
public void show() {
window.setVisible(true);
window.setExtendedState(Frame.NORMAL);
plot.requestFocus();
}
public void destroy() {
super.destroy();
window.dispose();
}
public void createGUI() {
super.createGUI();
//... Create a window (JFrame) and make applet the content pane.
window = new JFrame("Have Fun!");
window.setContentPane(this);
window.pack(); // Arrange the components.
// window.setSize(900, 700);
// setSize(900, 650);//800, 550);
// if (myBaseApplet.getToolkit().isFrameStateSupported(JFrame.MAXIMIZED_BOTH)) {
// window.setExtendedState(window.getExtendedState()|JFrame.MAXIMIZED_BOTH);
// System.out.println(" extended state supported!");
// }
// else {
Dimension D = myBaseApplet.getToolkit().getScreenSize();
D.height=(int)(D.height*.95);
window.setSize(D);
// }
Dimension d = window.getSize();
window.setSize((int)(d.width*.8), (int)(d.height));
super.start();
window.setVisible(true); // Make the window visible.
super.Resized();
window.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
}
public java.applet.AppletContext getAppletContext() {
return myBaseApplet.getAppletContext();
}
public java.net.URL getDocumentBase() {
return myBaseApplet.getDocumentBase();
}
public java.net.URL getCodeBase() {
return myBaseApplet.getCodeBase();
}
public Image getImage(java.net.URL url, String name) {
return myBaseApplet.getImage(url, name);
}
public String getParameter(String arg) {
return myBaseApplet.getParameter(arg);
}
}
Paul