Converting a Java program to an Applet

I'm trying to learn Java and the Crimson XML parser.

There is an example program which counts XML tags.

(http://xml.apache.org/crimson/)

I've got it running, but I'd like to convert it to an applet.

I figured all I had to do was change the main class to extend 'Applet', and change 'main' to 'init'.

The problem is that the main class already extends something else important, to wit:

public class SAXTagCount extends DefaultHandler

Can I just wrap this whole program in a new class which extends applet?

Is there another or a better way to do it?

[613 byte] By [--Harveya] at [2007-11-27 9:28:07]
# 1

> I'm trying to learn Java and the Crimson XML parser.

> There is an example program which counts XML tags.

> (http://xml.apache.org/crimson/)

>

> I've got it running, but I'd like to convert it to an

> applet.

> I figured all I had to do was change the main class

> to extend 'Applet', and change 'main' to 'init'.

>

> The problem is that the main class already extends

> something else important, to wit:

>

> public class SAXTagCount extends DefaultHandler

>

> Can I just wrap this whole program in a new class

> which extends applet?

> Is there another or a better way to do it?

AFAIK, a child class extends from one and only one other class, the parent (or super) class. Java does not allow multiple inheritence, and in my opinion, for good reason.

This does not mean that all hope is lost. Your applet can only extend JApplet, but it can and should contain variables of other classes including perhaps a class that extend a DefaultHandler or whatever it is you need. I think that this is the way you should go.

Any other solutions or corrections are welcome.

petes1234a at 2007-7-12 22:31:56 > top of Java-index,Java Essentials,New To Java...
# 2

> I'm trying to learn Java and the Crimson XML parser.

> There is an example program which counts XML tags.

> (http://xml.apache.org/crimson/)

>

> I've got it running, but I'd like to convert it to an

> applet.

> I figured all I had to do was change the main class

> to extend 'Applet', and change 'main' to 'init'.

>

> The problem is that the main class already extends

> something else important, to wit:

>

> public class SAXTagCount extends DefaultHandler

>

> Can I just wrap this whole program in a new class

> which extends applet?

> Is there another or a better way to do it?

Yes, you can easily build a new class that extends JApplet and contains your app:

class MyApplet extends JApplet {

private MyOtherClass moc;

public void init() {

moc = new MyOtherClass();

this.add(moc);

// blah blah blah and a little bit of blah.

// and maybe some more blah here.

}

}

(Assuming, of course, that your MOC is a "graphical component."

Navy_Codera at 2007-7-12 22:31:56 > top of Java-index,Java Essentials,New To Java...
# 3
Did SaxTagCount have a GUI? How were you expecting the applet to work?
paulcwa at 2007-7-12 22:31:56 > top of Java-index,Java Essentials,New To Java...
# 4
Thanks Pete!It got through the compiler with no syntax errors and ran without runtime errors! Now all I have to do is to get it to do what I want! <G> Harvey
--Harveya at 2007-7-12 22:31:56 > top of Java-index,Java Essentials,New To Java...
# 5

> Thanks Pete!

>

> It got through the compiler with no syntax errors

> and ran without runtime errors! Now all I have to do

> is to get it to do what I want! <G>

Harvey, please read the important notes above. I just want to make sure that you don't just copy and paste and hope and pray for the best. You will need t o carefully think this thing out.

petes1234a at 2007-7-12 22:31:56 > top of Java-index,Java Essentials,New To Java...