internet.com Developer's Forum
Magazines

webreference.com

Java Boutique

Search Engine Watch

PC Webopedia

The Web Developer's Virtual Library

Java Jolt
by David Wood

Toys, Toys, Toys

Just as someone once said, "let's start with the good news," we're going to begin this week with a look into the new toys in the AWT toy box. What do you get? And what's it like to actually use?

Perhaps in many ways indicative of the forward motion of the AWT, or it's lack (depending on your point of view), a good first representative of the the new components is the Tabbed Pane--bringing the manilla folder and filing cabinet look back to your screen in a burst of automated nostalgic glory.

Microsoft seems to have been responsible for repopularizing this particular graphical widget, and many of us have seen a quite a lot of it lately in configuration windows from all around the world. Truthfully, we're not too fond of the Microsoft version of it, but in principle, it's a compact and adequate way of organizing things.

Those of you who use Microsoft Windows 95 may see what we're talking about just by right-clicking on any of the empty desktop space on your screen, and then selecting Properties from the menu that appears. The Display Properties window that this brings up is a "Tabbed Pane."

Those of you who do not use Windows 95 may count yourselves lucky.

When we indicated that parts of the JFC are responses to parts of Microsoft's AFC, this is one of the things to which we were referring. Microsoft's tabbed pane Java interface component is called com.ms.ui. AwtUITabViewer. In Java it's com.sun.java.swing.JTabbedPane.

Those of who are really paying attention will recall the current API LayoutManager class java.awt.Ca rdLayout, which was clearly designed with this kind of application in mind, but without the graphical or structural trimmings.

JTabbedPane is used by instantiating it like you would any other, regular component, and using the addTab() method to add "pages" to it. Because its usage is specific and uniform, things such as a LayoutManager are already built into the Tabbed Pane.

The addTab method takes as arguments, at a minimum, a String and a Component. The string becomes the label inside an individual tab, and clicking on it will summon the associated component to the surface within the Pane--a straightforward enough way to subdivide the screen and break up an interface into smaller chunks. Accompanying the String can be something called a Glyp h--a very small, decorative graphical icon. We'll cover Glyphs in more detail later on.

Of course, like all the other components in the Swing toy box, you can change its appearance with a single call to the setUI method.

There's also the matter of the pane's state information. Touching once again on the separation between appearance, behavior, and content, Swing keeps the information represented by components in separate types of objects.

JBu tton, for instance, has a Button Model, which keeps track of the Button's state: whether it's pressed, released, or "grayed out" (disabled). It would also "notify" various objects registered to listen for changes in the button state by calling methods on them.

Tabbed Panes use a model object to keep track of, in this case, which tab is selected. This is a simple treatment for a probably seldomly used feature. The model, as we've described briefly, is simply a class that implements a model interface. In this case, SingleSelectionModel is the interface in question. It's used just for keeping track of a single number--the index number of whatever single item is selected. In this case, the index number of whatever tab is currently being displayed.

Keep in mind that this is only an interface--a common misconception is to think of the "model" object as only a placeholder. Any of the other objects in your program could implement the interface and be directly updated by a JTabbedPane object. Alternately, a relevant object in your program could implement a Listener interface and register itself to be notified by the event-handling mechanism. And for situations where neither approach is practical, we've noticed the presence of shortcut methods within the JTabbedPane class itself for directly getting and setting the tab index.

Of course, all of this model-based functionality is largely done in the name of thoroughness and to be consistent with other Swing components. The most likely situation is that you don't care at all what tab the user has selected, and it's the components on the pages themselves that your program is concerned with. In which case, the JTabbedPane does pretty much exactly what you want it to do. It's a fire and forget user-interface organizer.

The additional, unseen elements to JTabbedPane are the inherited bits of Swing infrastructure that provide, for example, better facilities to navigate through the Java AWT components with only the keyboard (much as Windows 95 do now in their native programs), or the debugging hooks that we've mentioned earlier.

This is Swing at its most elegant and self-contained. JTabbedPane, and the few other interface shortcuts like it, are deceptively simple on the outside, and when added into your application's display really do just work, requiring very little thought--hence, we call them the toys.

Of course, fun as they are, many would say they're the least of Swing's forthcoming contributions, and we're about to look into just what they mean. Join us again next week for more.

Past installments of Java Jolt


http://www.internet.com/
Web Developer® Home Over a dozen topics in detail Live Chat Downloads Book and Product Reviews Threaded Discussions How-To/Articles/Links Developer Daily News Subscribe Search Corporate Information Advertise Events Publications internet.com Home