I don't quite get this. I downloaded a class, put it on my website, and then created an HTML page with the basic applet tags, and the app showed up just fine.
But when I downloaded the source code and compiled it myself, all that shows up on my site is a blank applet with the error on the bottom of the browser, "start: app not initialized."
I tried writing a simpler code, such as
import java.awt.*;
import java.applet.*;
public class GuiExamplar extends Applet {
Button okButton;
public void init() {
setLayout(null);
okButton = new Button("A button");
okButton.setBounds(20,20,100,30);
add(okButton);
}
}
And it does the same thing. It compiles just fine, but doesn't show up on my browser.
Actually, any of my browsers. I tried Opera, Firefox, and Chrome. But the applet just shows a blank gray square.
This class is not launching as an applet as it does not extend JApplet or Applet.
Even when this is fixed, nothing will appear in the applet client. Applets do not call the main method but rather init. Also don't create a new Frame for applet components - add them to the applet container itself.
Related
I am trying to load an image into my applet with the following source code:
import java.awt.*;
import javax.swing.*;
import java.applet.*;
public class plum extends JApplet {
Image img;
public void init() {
img = getImage(getCodeBase(), "waves2.jpg");
}
public void paint(Graphics g) {
g.drawImage(img, 0, 0, this);
g.drawString("I am in the Applet", 35, 15);
}
}
I am using the policytool program to try and allow the applet to "read" the jpeg image file and show it in my web browser.
At the moment I receive the following error when trying to execute my applet.
I tried to configure with the policytool but I still receive the same error when running the applet. I set the codebase in the policy tool to the directory my html file is in that launches the applet. I used the file:///dir/dir1/dir2 type of syntax instead of http:// or https:// for the CodeBase field in the policy tool.
Does anyone know how I can fix this?
UPDATE
I have configured the policytool like so:
I have also configured it so that the codebase has all the permissions. In either case the picture does not show up, but the error is not thrown. Maybe I fixed it I really do not know. The picture does not show up but the error message is no longer shown saying that there is a permissions problem with reading the file.
I have a JFrame application working nicely. However now I'd like to run it on the web as an Applet. This is what I've done:
import MyPackage.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class MyName extends JApplet
{
public void init() {
setSize(600,450);
new MyName()
}
public MyName() {
JShellFrame frame = new JShellFrame(true, null, null);
frame.setVisible(true);
}
}
How can I make an html file to run this applet? Also, I have an external jar file that the applet will need. Does the applet not need a main method?
Check out Getting Started With Applets. It covers relevant methods and life cycle. It mentions main method as well:
Unlike Java applications, applets do not need to implement a main
method.
Deployment section covers HTML file details. For dependency jars you can specify more than one jar in archive attribute of applet tag.
However now I'd like to run it on the web..
Then drop this nonsense and launch the frame from a link using Java Web Start. I say 'nonsense' for two reasons.
JWS has existed since Java 1.2, & has been discussed in these forums several times in the last few days in regard to applets. Seems you are not doing much research.
Of the 'gargantuan' 2 lines of applet code code shown above, one of them is ill-advised and the other is either pointless or would risk creating a stack overflow error (could not be bothered trying it to find out which).
here's some html that will work:
<applet archive = "appName.jar" width = 900 height = 506 code = "main.class"/>
You can change the width and height according to the size your app needs. Also make sure to put the url as a path to the jar.
I have a Java applet that presents a JButton that allows users to navigate to another URL (which has a feedback form).
Problem: when I navigate to the form in Safari from the applet, typing into the form is garbled or impossible (only every 10th keystroke or so is actually entered).
Manually terminating the Java Web Plug-in for Safari fixes this immediately. I assume that I am somehow not releasing resources properly in my applet. I am aware that cleanup should be performed in the stop() method, but I'm not sure what resources I am failing to release that could cause this kind of behavior.
Here is the code that browses to the URL:
final JButton btnLaunch = new JButton("Go to survey");
final myJApplet mj = this;
btnLaunch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String url ="www.mywebform.example";
try {
getAppletContext().showDocument(new URL(url));
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
finally {
SwingUtilities.getWindowAncestor(btnLaunch).dispose();
mj.stop();
}
}
My best guess is that somehow Safari is mishandling resources by passing them to some Java ActionListener. However, including:
btnLaunch.removeActionListener(this);
in a finally block does not seem to help. I have other action listeners in my applet that I know are not explicitly destroyed, but I was under the assumption that generally this wasn't a problem as they are supposed to be disposed of automatically.
EDIT: Checking through my code, it seems I only have MouseListener or ActionListeners (on JButtons). So it is even more baffling that I'm having a keyboard issue. I tried manually removing them all in the stop method of the applet (which I manually call in the finally block), but it didn't help. MouseEvents and ActionEvents shouldn't continue to run upon navigating away from the page!
EDIT 2: Further information: having the applet open a new window which then contains a link to the final survey also does not work. However, making the link open in a new window (using html's target="_blank") seems to correct the problem. So for a work around, I have the applet direct users to a splash page which thanks them and then presents a link for the final survey. This is annoyingly kludgey, and doesn't answer the original question, but for now it seems like what I will be going with.
I have a JFrame application working nicely. However now I'd like to run it on the web as an Applet. This is what I've done:
import MyPackage.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class MyName extends JApplet
{
public void init() {
setSize(600,450);
new MyName()
}
public MyName() {
JShellFrame frame = new JShellFrame(true, null, null);
frame.setVisible(true);
}
}
How can I make an html file to run this applet? Also, I have an external jar file that the applet will need. Does the applet not need a main method?
Check out Getting Started With Applets. It covers relevant methods and life cycle. It mentions main method as well:
Unlike Java applications, applets do not need to implement a main
method.
Deployment section covers HTML file details. For dependency jars you can specify more than one jar in archive attribute of applet tag.
However now I'd like to run it on the web..
Then drop this nonsense and launch the frame from a link using Java Web Start. I say 'nonsense' for two reasons.
JWS has existed since Java 1.2, & has been discussed in these forums several times in the last few days in regard to applets. Seems you are not doing much research.
Of the 'gargantuan' 2 lines of applet code code shown above, one of them is ill-advised and the other is either pointless or would risk creating a stack overflow error (could not be bothered trying it to find out which).
here's some html that will work:
<applet archive = "appName.jar" width = 900 height = 506 code = "main.class"/>
You can change the width and height according to the size your app needs. Also make sure to put the url as a path to the jar.
I have a java applet that I've written and have been running it for quite a while. In the applet I have a bunch of JLabels that use HTML in their text content (which is allowed, and has been working for years). The main reason I use HTML is to allow line breaks in JLabels.
The issue: fairly recently the JLabels that have HTML in them (some don't) stopped displaying their text. I think it might have something to do with the recent java update (Java SE 6 Update 22, on 2010-Oct-12) not 100 percent sure, but the problems did seem to start around then. Maybe a bug has been introduced? or feature removed?
I tried with a JEditorPane and seemed to have the same issues when the content was HTML.
Also something important to note is that the first time you load the applet (first time your java runtime starts) it works FINE, but if you refresh the webpage then it has the issues as described.
Does anyone have similar issues? anyone have any insights? or am I just doing something dumb?
I made a very simple test applet and I can reproduce the issues with 100% regularity (remembering that the first time the runtime loads it will work fine, only successive refreshes will cause the issues):
[HelloWorldApplet.java]
import javax.swing.*;
public class HelloWorldApplet extends JApplet
{
public void init()
{
this.add(new JLabel("Hello Everybody (Text)"), java.awt.BorderLayout.NORTH);
this.add(new JLabel("<html><body>Hello Everybody (HTML)</body></html>"), java.awt.BorderLayout.SOUTH);
}
}
[HellowWorldApplet.html]
<html>
<title>A Test</title>
<body>
<applet code="HelloWorldApplet.class" width="320" height="120">
You need Java
</applet>
</body>
</html>
PS. I've been mainly testing using Chrome but I did breifly try in IE 8 also.
Also I (obviously) have the latest Java SE 6 update 22 installed as my run time, and have the matching update for my JDK. I compile the above test applet using simply: "javac HelloWorldApplet.java"
Wasted an afternoon looking for the cause of this, see the following bug report http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6993691
The workaround in the bug report is:
import javax.swing.*;
import javax.swing.text.html.parser.ParserDelegator;
public class HelloWorldApplet extends JApplet {
public void init() {
ParserDelegator workaround = new ParserDelegator();
applet.add(new JLabel("Hello Everybody (Text)"), java.awt.BorderLayout.NORTH);
applet.add(new JLabel("<html>Hello Everybody (HTML)</html>"), java.awt.BorderLayout.SOUTH);
}
}
Sounds from the description like a threading issue.
Unfortunately applets are not constructed or have their lifecycle methods called on the AWT Event Dispatch Thread (EDT).
Technically your constructor and init code should be executed inside java.awt.EventQueue.invokeAndWait.