Sunday, January 4, 2009

Applying the lesson

After completing the introductory trail Learning Swing with the NetBeans IDE, I decided to apply the lesson by building an applet of my own. Like the Celsius converter, the applet included a text box, a label, and a button, but it also needed something more. Whereas the Celsius converter takes input from the user and processes it to produce a directly related output, I wanted my applet to generate some output of its own, and then respond to the user's response to the initial output.

At this point I ask: Am I alone. The answer is definitely not, because people have been doing what I am doing since computers were invented. But am I in a minority of Java users? I guess if I think of the majority of forms on the web, they certainly kick off with mainly empty test boxes, and then they respond to what is written in the text boxes, initially with the application of a business rules layer, and then perhaps by writing to a database or generating a message for someone.

My applet is education based. Rude people call it drill and practice, and perhaps in this first Java version it is. I have seen similar functionality out there, but I don't know exactly how they store data on the fly. Perhaps they don't. Perhaps everything is dredged from or written to a database. However other people do it, I could not see from the lessons read so far, how to do it myself.

In VB, a form is an object, and just as objects in Java have fields to store data (or record state), so forms in VB can have data hanging off them, both visibly, in text boxes and labels, and invisibly in variables defined at the form level. So I was looking to define variables at the JFrame level, but NetBeans wouldn't have a bar of it.

After much soul searching and fruitless forum posting, I decided to create my own class, with all the fields I desired, and then put an instance of the class on the form. Everything seemed to work quite well. After creating and compiling the class, I was even able to drag it on to the JFrame. But because the lessons on packages had been so jolly confusing (even the name is confusing - I thought I needn't read those lessons properly until distribution time), I put my custom class in the wrong package and nothing worked.

The correct procedure is to put both the JFrame and the custom class into a custom package. A fudge which worked for me first time was to ignore all the warnings and put them both in the default package. What doesn't work is putting one in a custom package and the other in the default package.

No comments: