How to convert large Java application to an applet?

I have a large Java program with Swing GUI that has many packages and over 30 classes. I am using eclipse as an IDE. I have no problems running it as a Java application, but I would like to run it as an applet. If I try to run it as an applet in Eclipse I get a "ClassCastException".

What are the steps I need to take to be able to run my program as an applet?

Thanks in advance

[398 byte] By [WrightEQa] at [2007-11-26 12:53:25]
# 1
Do you run exactly the same classes as applet? Does your GUI class extend Applet?
Peetzorea at 2007-7-7 16:43:54 > top of Java-index,Java Essentials,New To Java...
# 2

> If I try to run it as an applet in Eclipse I get a "ClassCastException".

What exactly does Eclipse do if you "try to run it as an applet"? Does it change code?

What line throws that exception?

I suppose your large program instantiates a JFrame.

It add's components to the JFrame's contentPane.

Make the program add these components to a JApplet instead.

(Know that JApplet extends Panel.)

And use JApplet.init() as the starting point, instead of main(String[] args).

That's almost all you need to know, so:

1. find the main method of the program. Normally there is only one main method. It's signature must be public static main(String[] args).

2. from there, find where the JFrame or derivant is instantiated.

3. remove the JFrame instance (your JApplet will be instantiated automatically).

4. if a class in the program extends JFrame, so it adds components to its own contentPane:

- then you make that class extend JApplet instead of JFrame, and let it add the components to itself instead of its contentPane.

- else if the program just instantiated a JFrame (till step 3), then make a class that extends JApplet, and use the JApplet reference for everywhere the JFrame is referenced.

5. Write the public void init() function of your JApplet derivant.

tom_jansena at 2007-7-7 16:43:54 > top of Java-index,Java Essentials,New To Java...
# 3

For example if your program has a class for the main function and a class that extends JFrame: /* Main.java */

public class Main {

public static void main(String args[]) {

initApp();

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new MyFrame().setVisible(true);

}

});

}

public static void initApp() {

/* ... */

}

}

/* MyFrame.java */

public class MyFrame extends javax.swing.JFrame {

public MyFrame() {

initComponents();

}

private void initComponents() {

jLabel1 = new javax.swing.JLabel();

jButton1 = new javax.swing.JButton();

jTextField1 = new javax.swing.JTextField();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setText("jLabel1");

getContentPane().add(jLabel1, java.awt.BorderLayout.CENTER);

jButton1.setText("jButton1");

getContentPane().add(jButton1, java.awt.BorderLayout.NORTH);

jTextField1.setText("jTextField1");

getContentPane().add(jTextField1, java.awt.BorderLayout.SOUTH);

pack();

}

private javax.swing.JButton jButton1;

private javax.swing.JLabel jLabel1;

private javax.swing.JTextField jTextField1;

}

then you do

step 1: The main method is in class Main.

step 2: After calling initApp(), the main method starts a thread that instantiates MyFrame which extends JFrame.

step 3: You can remove this entire thread, so in this case the main method will only call initApp().

step 4: Yes, A class in the program extends JFrame and it add's components to itself. That is class MyFrame. Let it extend JApplet instead of JFrame, and substitute getContentPane().add(...) for add(...). Lets rename it to MyApplet.

step 5: Now MyApplet must have a public void init() function and that will be the starting point of the program. Let it call the Main.main(String[] args) function, or move the code from Main.main to MyApplet.init and remove the Main.main function.

what I forgot to mention:

6. You should not use the constructor of JApplet derivants, so move that code also to public void init().

7. You better use a new thread and EventQueue.invokeLater for creating the components.

So the example, converted to an applet becomes:/* Main.java */

public class Main {

public static void initApp() {

/* ... */

}

}

/* MyApplet.java */

public class MyApplet extends javax.swing.JApplet {

public void init()

{

Main.initApp();

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

initComponents();

}

});

}

private void initComponents() {

jLabel1 = new javax.swing.JLabel();

jButton1 = new javax.swing.JButton();

jTextField1 = new javax.swing.JTextField();

//setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setText("jLabel1");

getContentPane().add(jLabel1, java.awt.BorderLayout.CENTER);

jButton1.setText("jButton1");

getContentPane().add(jButton1, java.awt.BorderLayout.NORTH);

jTextField1.setText("jTextField1");

getContentPane().add(jTextField1, java.awt.BorderLayout.SOUTH);

//pack();

}

private javax.swing.JButton jButton1;

private javax.swing.JLabel jLabel1;

private javax.swing.JTextField jTextField1;

}

tom_jansena at 2007-7-7 16:43:54 > top of Java-index,Java Essentials,New To Java...
# 4
Look under "Converting Applications to Applets" here: http://java.sun.com/docs/books/tutorial/deployment/applet/getStarted.htmlAlso: http://www.javaworld.com/javaworld/javaqa/1999-07/01-qa-applet.html
zadoka at 2007-7-7 16:43:54 > top of Java-index,Java Essentials,New To Java...
# 5

Hi,

I am interested in How to convert a large Java application to an applet.

Here is a link for the Java application I would like to convert to an applet :

http://ivtk.sourceforge.net/

I have read-searched-played all night long and it seems that it is impossible without re-writing a large part of the code. That's a problem as the source package contains 617 files and 54 folders.

Is there any Automatic way to do such thing ?

a+ =)

-=Finiderire=-

P.S. : I'm a french guy and I apologize for mostakes

finiderirea at 2007-7-7 16:43:54 > top of Java-index,Java Essentials,New To Java...