PHD Computer Consultants Ltd
... Java Notes 1
Last modified: 1 November 1996.

PHD's Java Notes and Comments, archived 8 October 1996.
Minorly updated: 1 November 1996 (how browsers stop and start applets; is a double-click event)
Page Contents On-site Links Off-site Links
Assorted comments and notes PHD's Java Resources JDK known bugs
Handling awt events in Applets I JDK 1.1
Handling awt events in Applets II
Browsers: how they start and stop applets
Mouse events
Applets and classes
HTML and Java Development Environments
GridBagLayout PHDUtils Java class
My questions Java article

Note that a long hand version of some of the points on this page are in Chris Cant's Java article.

Assorted comments and notes

Handling awt events in Applets I

Events are first passed to handleEvent(). Either handle them here and return true, or return super.handleEvent(). If super.handleEvent() is not called then the event is ignored!

super.handleEvent() passes the event to the appropriate delegated Applet event handler, then to the component. So the Applet action(), keyDown(), etc. methods are called. You should return false from keyDown(), etc. if you want a component, eg TextField, to receive the key.

Delegated event handler list: mouseEnter() mouseExit() mouseMove() mouseDown() mouseDrag() mouseUp() keyDown() keyUp() action() lostFocus() gotFocus()

Handling awt events II

Having received an awt event, how do you know which object it has come from? If looking at button events, then you can inspect the button text and compare it against what you think its text is. Otherwise you have to have an Applet variable for each component you create and see if the event target matches that component

A better technique (methinks) is to have an integer id for each Component of your form. PHD's GridBagForm class provides this facility wrapped round the GridBagLayout layout.
GridBagForm.getIdFromComponent() and GridBagForm.getComponentFromId() are used in event handling.
GridBagForm also has methods to handle tabbing between components, and to remember the current field when you mouse click to a new tab field.

Obviously you could subclass each field, eg MyButton derived from Button and have an id for each button.

Browsers: how they start and stop applets

A bit of a mess!. So listen carefully... this is what I think is happening.
Which browser is right?


Case 1: When your Applet is loaded, then

Case 2: If you move to a different page then come back, the Applet is normally not reloaded.

Case 3: If you re-select a link to the page with the Applet in, the Applet is normally not reloaded.

Note carefully that the static initialisation of other classes is only called once, as you would expect. So when init() is run again in case 3 make sure that your other classes are set up properly. Do not assume that when you enter init() that other classes have just been static initialised. You might want to provide a separate init() function which "re-does" the static initialisation.

stop() is also called once per visit exit (and not destroy()); So, is the applet's destroy() method ever called?


Case 3 does not happen, ie this case is the same as Case 2.


Design your applet carefully to cope with these cases.

PHD's HelpIndex makes use of start() to select the whole of main query text. This makes it easy either to add to it or to overwrite it.

Somewhat worryingly, Applets seem to continue to work after the user has left the web page. I think that they also keep the input focus, if they have it! I would have assumed that their threads were suspended at the very least.



handleEvent() is supposed to receive MOUSE_ENTER and MOUSE_EXIT messages whenever the mouse enters or leaves a component. (NB these events are also generated when the mouse enters the surrounding area; in this case the Applet component is the target).

N3.0 is the only browser to generate these events properly.

N2.x, IE3.0 and Appletviewer (JDK1.0) do not generate these events.

GOT_FOCUS and LOST_FOCUS are therefore only generated by N3.0 (although there seem to be too many events arriving).

Applets and classes

Currently, if you make another class then a separate file has to be downloaded from your web server, which can take some time.

This will be improved once JDK 1.1 is adopted. A Java Archive (JAR) file can contain multiple files (compressed), including classes, images, sounds and other files, meaning that only one download is necessary. Let's hope that there is a method of providing a JAR file for those browsers that understand them, and the ordinary files for existing browsers.

This discourages a programmer from making up classes for ordinary situations, eg a simple structure might be needed to group a record of items together. For an initialised array, then an Object[][] would allow you to initialise objects (but not simple types) although you would need to cast them to the required type. Eg an array of colours could be Object[][] colours = { { "white". Color.white }, { "red", }, } etc.

There is a sneaky way of avoiding an extra class in some situations (taken from Java by Example card example). If you want to represent individual items, and a single collection of them, then have the individual items as instances of your class, while the static class represents the collection. This works as long as you only want one collection. I use this approach for our GridBadForm class, where each instance represents the individual form components, while the static class is used to manipulate the whole form.

Another trick is to avoid having a class extending Thread. Get another class (eg your applet) to implement Runnable and do your thread stuff in run().


GridBagLayout seems prone to getting an ArrayOutOfBoundsException when laying out.

HTML and Java Development Environments

HTML Development
Chris Cant generally uses Microsoft Visual C++ 4.2's Developer Studio to write HTML files in raw text form. It does tag colour coding, and does not interfere with the text.
(Sin... after everyone has gone to so much effort to make it easier to do...)

HoT MetaL Light 3 may be deemed useful for longer articles. However it seemed to reject some existing pages that displayed OK. Useful spelling checker. If you give it an anchor name, it moves the </A> tag to an inconvenient place.

Frontpage seems too interventionist by half. I am not sure if I like all its clever "bots". You need to set up a stack of sub-directories to support it. It did not seem to support local development of files which were then uploaded to the real server.
I am not sure how it does its "connect to web server". What mechanism beyond HTTP does it use? Does it only work with Microsoft servers?

Microsoft Word's Internet Assistant may be useful. However we are still in the bygone era of Word 6 so it does not support most useful features.

Java Development
Chris Cant currently uses Symantec Cafe Lite. Very basic but it works.

Sun's Java Workshop looks kinda cute, but is unusable.
There's no simple list of files in your project; this is a must as it is what programming is all about: selecting files to edit. Can you edit more than one file at once (in different windows)?
The browser is annoying: keeping the mouse pressed down on the vertical scroll bar down arrow does not auto-repeat. Similarly, the down arrow key does not auto-repeat.
In the code editor pressing F1 while on a method name, for example, should bring up help on that keyword.
The source browser only lists non-private variables and methods.
Its JDK classes seem out of date. Eg Label.setForeground() does not work and GOT_FOCUS is not generated properly (see above).
In browser forms, Ctrl+arrow keys move back a web page, not back a word.
I have seen other people's comments complaining at a lack of documentation.
Seems pretty slow on my NT4.0 P133 32MB.
When selecting files or directories, there is no disk browser dialog box (File Open).
Whatever nice features there are, the basics aren't there. It may be good for UNIX people, but PC people expect more...

I have not tried Microsoft's J++, Borland's Latté, Symantec Café or Aimtech's Jamba. What is the ++ in J++?

The following development tools have recently been announced:

Other Java Tools
Objective Computer Systems' RAD tool for applets, as used for NatWest bank (UK: 0171 315 6510)

Iona has a Java version of its object request broker Orbix Web 2.0 (+35 316 625 255)

Object Design has a Java database PSE-Pro, a lightweight persistent storage engine (UK: 01344 458200).

My questions

PHD Home page.