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.
Related
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.
EDIT: To those idiots who negged this question merely over the title, the button is clearly meant to be pressed if the question is out of the blue, without any effort put into it at all. I have researched and I have asked, and I have tried. All I am asking for is help.
It's hardly necessary to say just how much work I have put into trying to find a solution to my problem - I have asked questions, Googled, read documents, you name it, but all to no avail.
What I want to do is something I thought I could figure out within minutes: How to run images with JApplet, and use these images in the paint(Graphics g) function. I am running the JavaProject.html file from the build (also known as bin) folder, and it is from the file system, not HTTP. I did not include the "package" line in my code as well.
A recap of my journey is that the following have not worked for me:
This is my HTML file:
<html>
<head>
<title> My First Web Page </title>
</head>
<body>
<applet code="JavaProject.class" width="500" height="600">
</applet>
</body>
</html>
This method gives me the "Access Denied" "java.io.FilePermission" "Image.jpg" "read" Error. Needless to say, trying to work with images on a website does not work either. This one is one of the more frustrating ones because it works to with other people, yet not for me.
import java.applet.*;
import java.awt.*;
public class JavaProject extends JApplet
{
Image image;
public void init()
{
image=getImage(getDocumentBase(),"/Space.gif");
}
public void paint(Graphics g)
{
super.paint(g);
g.drawImage(image,20,20,this);
}
}
So that one didn't work. They suggested the getResourceAsStream method, so I tried that.
Image image;
Exception lastException=null;
try
{
image=ImageIO.read(getClass().getResourceAsStream("/Space.gif"));
}
catch (IOException ex)
{
lastException=ex;
}
But this one ended up giving me the "Illegal Argument Exception" input=null! Error.
This is my file arrangement: http://oi61.tinypic.com/5ohydc.jpg
Most other methods do not really work or are just far too complex to write down here, but none seem to work. I ask then, is my last resort just to get this thing signed? I have no idea how to go about doing that, and I think it's ridiculous that I even have to go through the trouble just to display images on my JApplet.
I have really lost all faith, and if this is to be fixed no doubt it will take enormous patience, but I would really appreciate any help. I am new to Java, so I can't really discuss much technically, but I pick up from examples rather quickly.
The first method you attempted does work. The only change you need to make is to remove the slash ( / ) before the file name you are attempting to use. Here's your original code (which works fine) with that one character removed:
import java.applet.*;
import java.awt.*;
public class JavaProject extends JApplet
{
Image image;
public void init()
{
image = getImage(getDocumentBase(),"Space.gif");
}
public void paint(Graphics g)
{
super.paint(g);
g.drawImage(image,20,20,this);
}
}
Note that in order for this to work, your 'space.gif' image must be in the root directory of your project, alongside the Java file that references it, like in this picture:
See how the image is in the 'default package' directory where my JavaProject.java file is also located? Use the code above, make sure your image is in the right place, and your program will run successfully. Here's mine in a running state:
The reason that the second attempt is failing is that it can't find the resource at the path you gave. That causes getResourceAsStream to return null which then results in the exception message that you saw.
You say that the error message in the first case was "Access Denied" "java.io.FilePermission" "Image.jpg" "read" or something. But according to the code, you are not trying to load "Image.jpg". That might be the root of your original problem.
A related issue is that in the getResourceAsStream version, you are using the wrong absolute path. You've used the path "/Space.gif", but given where you have put the file, it should be "/javaproject/Space.gif". Alternatively, since you are calling getResourceAsStream as on a class in the same (javaproject) package that contains the file, you could also use a relative path; i.e. "Space.gif".
I have really lost all faith, and if this is to be fixed no doubt it will take enormous patience ...
Actually, the real solution is to carefully read the documentation for the classes / methods you are using, rather than replying on random examples you found on the internet.
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.
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.
OK so I started with this:
import java.awt.*;
import java.applet.*;
public class Oval extends Applet {
public void paint (Graphics g)
{
g.setColor(Color.red);
g.fillOval(50,50,70,70);
}
}
Saved it as Oval.java. Then I ran javac Oval and it spits out the class file. Linked the class file to my HTML and all was good. Then I tried to change the color of the oval to say Color.blue and everything was not so good. I just changed the code in the Oval.java file, then re-ran the javac Oval and the result was a "new" class file... but it's the output is the exact same.
Do I have to "reset" the memory space or something? I have tried for some time to get the answer but I simply lack the vocabulary to accurately ask the question.
Sounds like the browser is caching the class file. I would try
Running the applet using the Applet Viewer
If that works, then it's a browser caching issue (clear your browser's cache)
But if it doesn't look different, or like you want in the Applet Viewer, then it must be something in your local environment (either it's not building the source file you want, or the class is going somewhere unexpected)
It could be that your browser is caching the previous version of your applet. Try clearing / disabling your cache.
Maybe the old version is in your browser-cache? Try hitting the follwing keys on your page:
Windows: ctrl + F5
Mac/Apple: Apple + R or command + R
Linux: F5
You don't have to reset the java environment, ever.
You might have a browser window open, and your browser is attempting to be "smart" avoiding reload of a page which is already "fetched". Depending on the browser, you will need to learn how to clear the browser cache.