Converting a JFrame to a JApplet - java

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.

Related

Converting JFrame code to JApplet [duplicate]

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 unsuccessfully tried to use images with JApplet for TWO days. What have they done to Java? Where am I going wrong?

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.

How to ensure execution of latest version of class file?

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.

HTML no longer working in JLabel (and other components)

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.

Create a simple form application to edit a textfile

I think I need to restate my question ...
I want to create a SIMPLE form application that edits certain areas of one very specific text file. Though I have some web development experience, I do not want to create a browser based application. Basically I want to give a Desktop application a try and I am looking for some help to get started including suggestions for the language of choice. The application should run on Mac OS X. Besides there's no restriction: Java, Cocoa, Python, even a some interactive shell script would be ok.
If you are interested in the details, continue to read here, but not that my question is not LaTex specific...:
I have an automatically generated report file that contains LaTex Code. Now I want to build a little application that creates a form field for every section and it's header. The document contains only a few hundred lines and the should work the following:
\section{ This is were the header text should go inside the document }
\normalsize{ This is where the normal text should go}
The header / normalsize pairs occur 5-6 times within the document. All I want is a little GUI that allows user to edit between the curly braces without seeing any TeX code. I know that there's LyX and other WYSIWYG approaches to LaTeX – I do not want to reinvent the wheel. I just want to protect the auto-generated code a litte from users and make it a little more comfortable to them.
EDIT:
here's my very first try. I guess I should use PlainDocument instead of directly sending it, but I´ll figure that out, since I got plenty of help from trashgod with the editor / Text Component links. The major problem is to single out the content from \section{} and \normalsize{} stuff. Probably I will some regexp here. I need to get a new text area for every appearance.
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileReader;
import javax.swing.*;
public class basicFrame extends JFrame {
// Declare Variables
JScrollPane bildlauf = new JScrollPane();
JTextArea txtfeld = new JTextArea();
public basicFrame() {
super();
// Main window
setTitle("ReportEditor");
setBackground(Color.LIGHT_GRAY);
// components
try {
File datei = new File("report.Rnw");
FileReader in = new FileReader(datei);
txtfeld.read(in, datei);
in.close();
} catch (Exception e) {
System.out.println("Error !");
}
// setLayout(new GridLayout(2,2));
// Scroll Shizzle
bildlauf.getViewport().add(txtfeld, null);
getContentPane().add(bildlauf, BorderLayout.CENTER);
//txtfeld.setSize(500,680);
//add(txtfeld);
//this.getContentPane().add(txtfeld);
// close
addWindowListener(new WindowLauscher());
}
// event handlers...
protected static final class WindowLauscher extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
public static void main(String[] args) {
//Fesnter erzeugen und anzeigen, die main Sache halt
basicFrame hf = new basicFrame();
hf.setSize(500, 700);
hf.setLocation(100, 100);
hf.setVisible(true);
}
}
Thx in advance for any suggestions!
The TeX on Mac OS X wiki recommends jEdit, which supports plugins. LaTeXTools might be a good start.
Addendum:
I do not want to reinvent the wheel.
All I want to do is create a form application.
Although these goals are somewhat contradictory, you can always parse the file and use a suitable JTextComponent for each editable section. Here's an overview of Using Text Components; see Text Component Features if you want to create your own editor kit, as discussed in Customizing a Text Editor.
Addendum: In addition to the tutorial, you might look at this text field layout example. Also, consider a two-column JTable, which would allow you to cleanly separate your document model from your form view.
Addendum: A few notes on your code.
Class names are usually capitalized, e.g. BasicFrame.
Don't extend JFrame if you're not changing it's behavior; JPanel is a good container for other components, and it can be displayed in a JFrame.
Always buid your GUI on the EDT.
Keep you view and model separate.

Categories

Resources