by David Wood

Pour On The Java.  Hold The Programming.  Click Here.  Jamba.

Creating a Consistent Java Interface

One of the myriad of problems associated with systems like Java that are designed to run on any number of incompatible platforms is consistency of interface.

It would have been unreasonable if not impossible for the Java engineers to create a single set of "java" interface widgets and implement them on every platform along with the Java runtime package. Instead, the sensible choice was made to use the tools of the existing windowing environment available on whatever host operating system was in use.

Technically, this may not be the shortcut it sounds like. It's worth using the graphical interface routines of the host machine for a few reasons, not the least of which being that the consistency of the user's interface is preserved.

When we say this, it means that consistency is preserved for the user; your applets have some of the same feel that the user's regular applications do since they share the same interface routines for drawing buttons, windows, and the like. When you create a new Button() in your applet, you achieve the same effect on your user's screen as if he or she had been running a "native" application that had used the ordinary operating system calls to produce a button. This way of doing things isn't a surprise to people who've spent time designing Web pages in recent years. Most have gotten used to anticipating the different appearances and functionality of their HTML forms on different platforms as Web browsers themselves take this same approach.

Of course, this means that for you, the programmer, consistency is not preserved. Although Java has selected and implemented a variety of objects in the window toolkit that are common and similar among all the platforms Java has thus far been implemented on, this in no way means that they are especially similar beyond the basics.

Most obvious and immediate are the differences in size and shape between the interface widgets of various windowing environments. Depending on the user's default and available fonts, screen resolution and color depth, and choice of operating system, your carefully designed interface may look perfect or may fall literally to pieces.

This is why, of course, Java contains such an intricate system for laying out interface objects, and why we've spent so much time in the past few weeks talking about it. All too frequently, when designing applications for any purpose, presentation is everything.

Layout Managers are fundamentally about describing how you want your objects to appear in relation to each other, as opposed to just choosing where you want them to appear. Keep this essential consideration in mind as we look at the often confusing GridBag Layout.

GridBag is intimidating because of its complex interface; in addition to add()ing your AWT components, you must create and use a GridBagConstraints object to maintain what are basically formatting instructions for this Layout Manager, and call the setConstraints method on the layout manager to put them in place before every add.

The basic principal is that every interface component that you might want to add has a set of constraints associated with it. You set them by modifying your constraints object and then record them with the Layout Manager by calling:

mygridbag.setConstraints(MyInterfaceObject,CurrentConstraints);

There are a number of constraints this system gives you to play with; for example, working within a grid metaphor, you can set how much space an object will take up in the display relative to the number of other objects in the same row or column. You can select how far your objects appear from each other and from the borders of your window. You can specify how you want space distributed among columns and rows, and how you want the entire system to behave if it's in a window that happens to be resized. The Sun tutorial section on constraints provides all the details.

Designing user interfaces that are consistent, attractive, and functional across all conceivable platforms is an art. Although not perfect, the Layout Manager system provides some often very convenient tools for achieving this end. But enough theory. Next week we're going to look at an example of a complex interface using the GridBag. Be sure to check us out.

Past installments of Java Jolt

http://www.internet.com/