Tuesday, February 24, 2009

NetBeans App to Applet Conversion

In an earlier blog, I explored the possibility of moving code from one NetBeans project to another. To cut a long story short, it was pretty much a case of dragging source files and classes from one project and dropping them into another. This experiment was with two projects of the same type (both applications), but I was hoping I could try the same technique dragging code and objects from a desktop application to a web application. And again to make a long story short, I couldn't make it work.

NetBeans is very clever the way it helps you set things out on a form (or jframe, as they call it) with everything nicely lined up and the right size. It is also very clever the way it writes a lot of code, all with the right syntax. Against this, I find that if you want to do anything beyond plain vanilla, the help files are singularly unhelpful, and the tutorials seem simply to replicate the help files. I also find it annoying that I am locked out of code in NetBeans, and to make it worse, if I open a NetBeans source code file with a simple text editor like Notepad, everything is jumbled up together, and frankly, unreadable.

I therefore searched the web for a simple Java Editor. I wanted something that would display a source file nicely, without being as big, cumbersome and restrictive as NetBeans. Number 2 on a Google search was jEdit, which calls itself a programmers test editor. This was exactly what I was looking for. It was free (why pay for something which does less than NetBeans, which is free?), and easy to download and install. And it read my source files beautifully.

Meanwhile, as a belt and braces long shot, I posted a question in the Java Tutorial forum asking about App to Applet migration in NetBeans. I got an excellent reply, which explained that I had to create an Applet in my package, and lift the objects one by one from my App. Putting the applet in the existing package was a particularly clever suggestion, because it enabled me to copy not only regular objects like text boxes, but also the custom object which stores my variables.

The post also provided a link to a NetBeans documentation article, which explained how then to integrate the Applet into a web application. Specifically it enabled me to post it on my (privately hosted) web test page. It might have taken me a hundred years to find that article, and it was very nice to be pointed straight at it.

Tuesday, February 17, 2009

Converting Applications to Applets

As luck would have it, the bit that I am particularly interested in, Converting Applications to Applets, crops up quite early in the getting started with Applets lesson. The section opens with a paragraph on the technical differences between an application and an applet, and then goes on to outline what it calls the "basic steps ... to convert an application program into an applet program.". I quote:

  • You need to create a subclass of java.applet.Applet in which you override the init method to initialize your applet's resources the same way the main method initializes the application's resources.
  • init might be called more than once and should be designed accordingly. Moreover, the top-level Panel needs to be added to the applet in init; usually it was added to a Frame in main. That's it!

When I read the words "That's it" after two short bullet points, I am lead to believe that what is being explained is pretty simple, and that the conversion will comprise inserting a few words here and there. But after reading these bullets I felt none the wiser.

I therefore take advantage of the invitation to compare the source code of an application with that of an applet. I open both source code files and paste the content side by side into two columns of a spreadsheet. This makes the comparison much easier, and I can begin see the differences referred to in the text.

The first bullet point above opens with: "You need to create a subclass of java.applet.Applet ... ". And at the top of the applet source code, where packages are imported, I note an extra line:

import java.applet.Applet;

And after that, where the application declares the following class:

class SwingUI extends JFrame   
implements ActionListener {

the applet declares:

public class ApptoAppl extends Applet   
implements ActionListener {

So far so good, but as I skim through the rest, simple comparisons escape me. I therefore turn back to the lesson, where I find the two initial bullet points (which were supposed to be "it") have been replaced or embellished but five more:

  • The applet class is declared public so appletviewer can access it.
  • The applet class descends from Applet/JApplet and the application class descends from Frame/JFrame.
  • The applet version has no main method.
  • The application constructor is replaced in the applet by start and init methods.
  • GUI components are added directly to the Applet; whereas, in the case of an application, GUI components are added to the content pane of its JFrame object.

The first two of these seems to correspond to the first subclause of the first of the two bullets, and to be illustrated by the snippets of code shown above. The third seems to correspond to the last subclause of the first of the two bullets, and is confirmed when I scroll to the bottom of the source files, and note that the main method is certainly missing from the applet.

The fourth bullet refers to the "application constructor". I'm not sure that my application has a constructor, but the term rings a bell from the theory lessons, so I'd better go back and check what it means. Turns out that I wrote quite a bit on the subject in September, and by the look of it I didn't enjoy those lessons any more than this one.

Feeling bored, I compile both source files into classes and run them. To my surprise both run OK. This was quite interesting for me as an exercise because I had never before applied Swing outside NetBeans. It is also a bit easier for me to interpret the code, knowing what the app looks like. And even the constructor makes a bit more sense in the context of a GUI. More sense at least than it did with my bicycle. The constructor applies values to variables, or objects, and when the objects are components on a form, the name makes more sense, because it "constructs" the form.

In the application the "constructor" puts a label and a button on something called a jpanel. In the applet, the init method appears in place of the constructor, and that is where the text box and button are created. It is irritating in an example like this where one is trying to compare two things that the order is inconsistent. In the App, the button and label are created before the jpanel. In the applet, the jpanel is replaced by a BorderLayout, which is a substantive (but unlisted and unmentioned) difference between the two, but to confuse the learner, in the applet, the BorderLayout is defined before the button and label. To confuse the reader even further, bullet point five refers to a JFrame, of which there is none in the application.

In the applet, the Init method is followed by the Start, Stop and Destroy methods. These all include a System.out command. It is not clear whether these are required, or simply put in as a distraction.

Further down the page, the button ( public void actionPerformed(ActionEvent event) code is identical in both App and Applet. This is quite heartening, because in most apps (and certainly in mine) that is where the crunchy code is written. So at least that much can be copied and pasted.

In summary, I wouldn't really call this process converting an application to an applet, so much as comparing and contrasting an application with an applet. Certainly I am not convinced that there is an easy way to "convert" my application to an applet. I can see how I can reuse some of my code, but I think the first step will be to create an applet from scratch, and then perhaps I can splice in reusable code.

Thursday, February 12, 2009

The Hello World Applet

Editorial Note: I have removed some of the links on this page because the tutorial has been updated; the old links don't work, and the applet code has been changed since this blog entry was made.

So the "next" lesson in the trail is Getting Started with Applets. I hate hello world, but after the last lesson I'm quite pleased to see it. At least it provides a scrap of code to play with. If I can embed this applet into my web page, it will improve my understanding, and if it works, it will make me feel better. So to facilitate the process, I check out the source code for the page.

The first thing I notice is that the code calling the applet on this page is completely different to the example on the previous page. The example given was:

 <applet code=AppletWorld.class

width="200" height="200">
</applet>

But the code used to call the Hello World applet is:

<applet code="HelloWorld.class"

codebase="examples/"
archive
="helloWorld.jar"
width
="150" height="30">
</applet>

That looks more like a jnlp file than the example Linkgiven, and it certainly makes it harder to copy and paste. The first question is why, if we need a jar file to run this applet, was it not mentioned on the all informative index page. And if there isn't room to tell the whole story on that page, why tell any part of it at all.

To test this, I'll compile Hello World from the source code given here, and try running it with the code given on the index page. I'll insert:

    <applet code="HelloWorld.class"

width="150" height="50">
</applet>

To my amazement, it works, not only on my server but also on my ISP hosted page.

Wednesday, February 11, 2009

Java Applets

There are more lessons in the Java Webstart Trail, but quite frankly the effort I have expended so far way exceeds any benefit. After many days of fiddling, I got my application to run using Webstart. But my commercial ISP does not support jnlp files, so I had to host the app on my own server, which is not available 24/7. And the end user is required to have Webstart installed. Frankly anyone, with the skill or the energy or the inclination to install Webstart, could just as well run the app from the command line.

So I shall now look at embedding an applet in a web page. This takes me to a thread entitled Creating and Deploying Applets with Java Plug-in, and the index lesson is simply entitled Applets.

It opens well: "This lesson talks about the basics of applets, advantages of applets over applications, how to load applets in a web page, how to convert applications to applets and how applets work". These are exactly the things I want to know. Let's hope the lesson delivers what it promises.

As usual for this tutorial, however, the lesson is completely back to front. It begins with the last thing I want to know, some boring details about the life cycle of an applet, and continues with the second last thing I want to know, how to load the applet into a web page. Why is this stuff fleshed out on the index page, while the interesting bit: "how to convert applications to applets" is nowhere to be seen, either as a heading to a paragraph, or as a link? The word "convert" appears only once: in the promise, but not in the delivery.

I now have to apply my (non-existent) psychic powers to guess in which branch of the trail I will find what I am looking for. No I'm not getting anything. I've tried chanting, I've tried laying my hands on the screen, but nothing is coming to me. I'll just have to plough through the trail in order until I find what I want.

I thought the whole point of hypertext was to provide links in context to help people find what they want. A good index page would have links embedded throughout the text, not just in the headings. If they only put links in the headings, people might just as well read a good book.

Monday, February 9, 2009

Windows 7 upgrade and networking issues

As a general rule, when I test an OS I do so on a clean drive, or at least an empty sector. But when I was browsing through the MS fora looking for ideas on the network dysfunction it seemed that a number of punters were using the upgrade option, and even that MS intended people to this. So I ghosted my working XP drive to an experimental drive, booted to that drive, and full of optimism, inserted my Win 7 DVD.

Everything seemed to begin well, but then after a few minutes a dialog box appeared with the message: Windows Setup experienced an unexpected error. To install Windows, restart the installation. Error code: 0x0. I am not surprised, but I am disappointed. This is pathetic. Vista was way better than this. I opted not to buy Vista, but I didn't hate it. I just thought it wasn't worth shelling out hundred of dollars. But Win7 seems to be a complete botch. The feeble opening pic, even the name seems to suggest that MS is losing heart, if not the plot.

But I shall continue. My day job is advising people on their computers, and "people" tend to buy computers in shops, and shops will sell computers with Windows 7 installed, because they know no better. So I shall continue to fiddle with Win7 until August, when the trial runs out.

I check the MS TechNet forum on Windows 7 Installation, Setup, and Deployment and search for error 0x0, but get only 14 hits, of which only a couple seem similar to my experience and only one had an interesting suggestion. Sadly the suggestion did not work for me.

During my first clean install I remember seeing an upgrade option. So I booted from the CD and selected Upgrade, only to be told that Windows needs to be running. How annoying is that? Why include the option and then announce that it can't be used. Does MS not have any shrinks on staff to advise them how not to annoy people?

My experimental HD has some empty space, so I create a new partition, and do a clean install on to that. By now I have had a reply to my post in the Windows 7 Hardware Compatibility forum on my Asus p5k networking problem, and the reply works. So now I can connect to the Internet and at least activate Windows 7. But when I start installing programs from my network library, more problems arise.

First I am invited to set up a Home Network Group. I don't want to do this. My computers use a workgroup called workgroup, and in any case I usually access shared resources with the run command. But when I am prompted for administrator permission to open a shared file (I am an administrator), I check out this Home Network Group. Then I am told that all participating computers have to be running Windows 7. How annoying is that? MS really have lost the plot here. XP was better than 98 because networking was easier and worked better. Vista was OK for me because most day to day tasks worked no worse than in XP. But this one is a monster. Some people compare Vista with Windows Me. For me, Windows 7 looks like it is going to be the next Windows Me. Certainly I shall use it for tinkering and testing only. My main working OS remains XP for now.

Saturday, February 7, 2009

Tinkering with the JNLP File

The lesson Deploying Java Web Start Applications includes the statement: "The following table describes the elements and attributes in the sample JNLP file". This statement is not true, because the table does not include the j2se element. However the lesson redeems itself with the following admission:

Note: This table does not include all possible contents of the JNLP file. For more information, see the Java Network Launching Protocol & API Specification (JSR-56).

Quite frankly, the lesson would be as useful if this note (or at least the link) were the only the only content in it.

The answer to my earlier question on the main attribute of the jar element is included in the specs, and it turns out that my "logical" guess was wrong. That line in my jnlp file should in fact read:

<jar href="ActiveMathJA002.jar" main="true"/>

But in fact when I substituted that line, the app still would not run. The error message said the application-desc tag was missing, which it isn't, but it tempts me to remove all the redundant crap, so the file looks as follows:

<?xml version="1.0" encoding="utf-8"?>
<!-- JNLP File for ActiveMath -->

<jnlp spec="1.0+"
codebase="http://202.65.70.207/myweb/"
href="AMJ002.jnlp">
<resources>
<jar href="ActiveMathJA002.jar" main="true"/>
</resources>
<application-desc main-class="AMJFormA002"/>
</jnlp>

That didn't help - same error message. So I'll try a file with one element (besides jnlp and xml):

<?xml version="1.0" encoding="utf-8"?>
<!-- JNLP File for ActiveMath -->

<jnlp spec="1.0+"
codebase="http://202.65.70.207/myweb/"
href="AMJ002.jnlp">
<application-desc main-class="AMJFormA002"/>
</jnlp>

It didn't work, but at least I got a different error. Now it is looking for information title. So I'll try:

<?xml version="1.0" encoding="utf-8"?>
<!-- JNLP File for ActiveMath -->

<jnlp spec="1.0+"
codebase="http://202.65.70.207/myweb/"
href="AMJ002.jnlp">
<application-desc main-class="AMJFormA002"/>
<information>
<title>Active Math Java Test</title>
</information>
</jnlp>

Now it wants vendor:

<?xml version="1.0" encoding="utf-8"?>
<!-- JNLP File for ActiveMath -->

<jnlp spec="1.0+"
codebase="http://202.65.70.207/myweb/"
href="AMJ002.jnlp">
<application-desc main-class="AMJFormA002"/>
<information>
<title>Active Math Java Test</title>
<vendor>Softway Interactive Education</vendor>
</information>
</jnlp>

Now it is looking for application resources - I was expecting that ages ago:

<?xml version="1.0" encoding="utf-8"?>
<!-- JNLP File for ActiveMath -->

<jnlp spec="1.0+"
codebase="http://202.65.70.207/myweb/"
href="AMJ002.jnlp">
<application-desc main-class="AMJFormA002"/>
<information>
<title>Active Math Java Test</title>
<vendor>Softway Interactive Education</vendor>
</information>
<resources>
<jar href="ActiveMathJA002.jar main="AMJFormA002"/>
</resources>
</jnlp>

And now it says it can't find the class file. I must say this is the most extraordinarily temperamental utility, and at this stage I think the effort far outweighs any benefit that I can see. Anyone with half a brain could simply download my jar file and run it from the command line without all this ridiculous palaver.

In desperation I started to compose a post for this forum. The forum is quite clever because if you use the code tags, in preview mode, it colours the text to highlight errors. And this enabled me to spot that I had missed a close quotes in the resources section. Also when I unzipped the jar file from my web folder, I remembered that when I was fiddling with packages, I had put the main class into a custom package. The finished file now became:

<?xml version="1.0" encoding="utf-8"?>
<!-- JNLP File for ActiveMath -->

<jnlp spec="1.0+"
codebase="http://202.65.70.207/myweb/"
href="AMJ002.jnlp">
<application-desc main-class="AMJFormA002"/>
<information>
<title>Active Math Java Test</title>
<vendor>Softway Interactive Education</vendor>
</information>
<resources>
<jar href="ActiveMathJA002.jar" main="AMJPackA002.AMJFormA002"/>
</resources>
</jnlp>

And it ran.

Friday, February 6, 2009

Windows 7

A review of Windows 7 belongs here only to the extent that I am running Java from Windows, and it is quite interesting to know how easy it will be to rebuild everything I had working in XP. I am building Win7 on a clean drive because I want to be able to go back to XP as simply as moving a cable from one drive to another.

My first impression was disappointing. I loved the default background pic in Vista - it was so clean and crisp. The default pic in Win 7 looks cheap and half rate. The impression is more like trying out a lame Linux distro than I would expect from a new product from MS. Fixing the pic was easy, but it was more disappointing still to find my network was dead.

I am using an Asus P5K. Asus is not exactly an unknown brand, and I bought the board 18 months ago, so how come Win7 didn't ship with the drivers? And of all the things to lose, the network is most annoying, because you either have to start fiddling with hardware, or you have to use both another computer and media (CD's or thumb drives) to fix the problem.

I have tested quite a few Linux distros, and while none of them has made me completely happy, from memory, none has failed to connect to the Internet on first install. It is an interesting turn of history that a beta release from the once mighty Microsoft, not only feels like a Linux distro, but so far is doing less well.

As an act of optimism, I install the Mobo CD. But the autorun setup program doesn't like the OS. I then browse through the CD to locate the Vista drivers for the LAN. As another act of optimism I try to run the setup file, but it does not like the OS. To be rigorous I let Win 7 hunt for the LAN driver with the CD in the drive, but it finds nothing, either on it's own, or being led to the Vista driver. I thought Win 7 was built up from Vista. Whatever, it is already more annoying.

As a final act of due diligence on this I check the Asus web site, but I am not surprised to see nothing for Win 7 for my mobo.