Archives
 
 
 
  Special
 
 
 
  About Us
 
 
 

Newsletter
Free E-mail Newsletter from BYTE.com

 
    
           
Visit the home page Browse the four-year online archive Download platform-neutral CPU/FPU benchmarks Find information for advertisers, authors, vendors, subscribers Request free information on products written about or advertised in BYTE Submit a press release, or scan recent announcements Talk with BYTE's staff and readers about products and technologies

ArticlesBuilding a Better Interface with Java


August 1997 / Core Technologies / Building a Better Interface with Java

Internet Foundation Classes lets you build Java applets with useful features such as drag and drop.

Andy Turk

If you've ever tried to build a significant application using Java, you have probably run into some of the limitations of Sun's Abstract Window Toolkit (AWT) that's provided with the Java Development Kit (JDK) 1.0.2. The AWT was actually designed for building small applets, but with the increasing popularity and acceptance of the language, many developers are going beyond simple applets to write ful l-blown applications in Java. If this sounds like you, then you should take a look at the Internet Foundation Classes (IFC), from Netscape, before starting your next project.

The IFC is a library of user interface (UI) widgets, along with a set of tools for building your own widgets. All the IFC code is written in Java that runs on top of AWT, so Java's promise of platform independence is preserved, as shown in the figure "IFC in a Java Application ." Just recently, Sun and Netscape announced that they are combining AWT and IFC technologies into a set of Java Foundation Classes (JFC) t hat implements an extendible look and feel to Java applications. JFC will be part of the next release of the Java Development Kit, thus making these interface classes a dependable standard that developers can rely on.

For those who want to experiment now, you can download the IFC from http://developer.netscape.com/library/ifc , and you can get the AWT from Sun at http://java.sun.com/products/jdk/1.1/ . This article will focus on the capabilities of IFC and compare it to the once-separate AWT.

Building a Better UI

Distinctive UIs stand out from the crowd by paying attention to detail and avoiding overuse of the same old "cookie cutter" buttons, fields, and list boxes found in most other applications. AWT makes such enhancements difficult because it provides few opportunities for going beyond the bare minimum. To be fair, AWT's emphasis is on event handling, whereas IFC concentrates on extendibility and attempts to impart a consistent appea rance across platforms.

For example, most AWT buttons look the same except for their titles. Without writing your own button class from scratch, it is difficult to change much more than the title of a stock AWT button. IFC's Button class, on the other hand, has a surprisingly wide variety of attributes that can be combined to make buttons more versatile and pleasing to the eye. Along with changing the button's title, you can also change the font and color in which the title is displayed. You can associate an image with the button and specify where the image should be displayed relative to the title. You can even use an animation instead of a static image through the use of a DrawingSequence class. If all these options still aren't enough, you can always override the draw method of the Button class to do exactly what you want.

Another example of the power of IFC is its TextView class. This class is essentially a mini word processor that lets you display and edit text using different fonts, colors, and sizes. With the TextView class, you can add in-line attachments that represent figures, file attachments, or other graphic elements that need to flow with the text. TextView even provides a conversion method that parses and displays HTML. Attempting any of this with standard AWT widgets is next to impossible. IFC also provides other UI elements that aren't found in AWT. A good example of this is the ColorChooser object that allows a user to display and manipulate colors.

Powerful Views

You may be wondering how IFC is able to do all these things. The answer is that IFC is built around a powerful UI element called a View object. A View is simply a rectangular area on the screen that knows how to draw itself, and it also knows what to do with mouse and keyboard events. Views are subclassed to provide widget-like behavior. For instance, Buttons , TextViews , and all the other widgets that c ome with IFC are subclasses of View . Thus you construct complete UIs by simply connecting several View objects together in a hierarchy. IFC Views are written entirely in Java and are used to implement all of IFC's standard UI components, as well as any components you write for your own application. What this means is that you, as an application programmer, have access to the same tools as the programmers who wrote the IFC widgets, and you can implement your own custom drawing and event-handling behavior if you need to. This is a big improvement over AWT's peer model, in which the widget methods are implemented in C, without any option to be overridden in Java.

Another benefit of IFC's View model is that it provides the tools to implement drag-and-drop capability. This is a type of UI where a user clicks on something and drags it with the mouse to another part of the screen, where an operation is performed. The ColorChooser object makes good use of this techni que: The user drags a color "chip" from it, then drops this on any View that knows what to do with colors.

Behind the scenes, the color "chip" is simply a tiny View object that knows how to track the mouse and draw a color. For example, suppose that you're writing a drawing application that needs to let the user apply a color to a shape. As a programmer, all you need to do is implement a few methods in your Shape object to accept a color that was dragged out of the ColorChooser . The code sample in "Dragging and Dropping a Color" shows how easy this is to do. Implementing drag-and-drop capability is nearly impossible with AWT alone because the standard widgets won't allow a color "chip" to be dragged over their part of the screen.

Designing a UI

One of the best things about IFC is its application builder, called Constructor, which streamlines the process of building a UI. With Constructor, you can design how the UI will look withou t writing any code. In this respect, Constructor is similar to many other application builders, such as Apple's OpenStep Interface Builder. However, Constructor gains extra leverage because it is tightly integrated with IFC. In fact, Constructor is a Java application and runs under any OS with a Java virtual machine.

Constructor won't help you write code --it has no built-in text editor. Constructor's primary purpose is to help you design how the application's interface is going to look. To do this, you simply drag IFC widgets from a tray and drop them onto the panel that you're building. From there you can reposition and resize the widgets, and you can set various display attributes.

Once you've positioned your widgets, the next step is to use the "Wire" mode to hook those widgets together. For instance, the Button object sends a message when it receives a mouse click, and you use Constructor to determine not only the message sent but also the object that receives it. You can even test out the user interface by running it from inside Constructor itself. Once your design is complete, Constructor saves the information in a file that's loaded at run time over the network.

The Catch

There's always a catch, and the catch with IFC is that it's big. The library of .class files that comprise IFC is nearly a megabyte in size. If you are writing a Java application that must be downloaded over a narrow data pipe, like a modem, you should think twice before using IFC. On the other hand, if you're planning to develop a full-blown application that's deployed on an Ethernet-based intranet, the size of the IFC class library won't be an issue.

Another factor to consider is that IFC comes with its own unique look and feel. IFC buttons look a little different than buttons on other platforms, and the same is true for scroll bars. Some aspects of the IFC UI are slightly quirky, like the corners of resizable windows. However, the graphic design is consistent throughout all the IFC w idgets, and they work well once you get used to them. It's this consistent appearance, combined with the Java virtual machine, that will help establish Java applets as an industry-wide standard. Users will know what behavior to expect, regardless of their platform of choice.


Dragging and Dropping a Color

public class Shape extends View implements DragDestination {
 protected Color myColor;
 public DragDestination acceptsDrag(DragSession session, int 
 x, int y) {
  if(session.data() instanceof Color) {
   return this;
  } else {
   return null;
  }
}
 public boolean dragDropped(DragSession session) {
  if(session.data() instanceof Color) {
   myColor = (Color) session.data();
   drawView();
   return true;
  } else {
   return false;
  }
}
}




IFC in a Java Application

illustration_link (29 Kbytes)

Internet Foundation Classes (IFC) implements a consistent interface and behavior for a Java application.


Andy Turk is the founder and president of Sarrus Software, Inc. (Burlingame, CA). You can reach him at andy@sarrus.com .

Up to the Core Technologies section contentsGo to previous article: Go to next article: Faster Internet AccessSearchSend a comment on this articleSubscribe to BYTE or BYTE on CD-ROM  
Flexible C++
Matthew Wilson
My approach to software engineering is far more pragmatic than it is theoretical--and no language better exemplifies this than C++.

more...

BYTE Digest

BYTE Digest editors every month analyze and evaluate the best articles from Information Week, EE Times, Dr. Dobb's Journal, Network Computing, Sys Admin, and dozens of other CMP publications—bringing you critical news and information about wireless communication, computer security, software development, embedded systems, and more!

Find out more

BYTE.com Store

BYTE CD-ROM
NOW, on one CD-ROM, you can instantly access more than 8 years of BYTE.
 
The Best of BYTE Volume 1: Programming Languages
The Best of BYTE
Volume 1: Programming Languages
In this issue of Best of BYTE, we bring together some of the leading programming language designers and implementors...

Copyright © 2005 CMP Media LLC, Privacy Policy, Your California Privacy rights, Terms of Service
Site comments: webmaster@byte.com
SDMG Web Sites: BYTE.com, C/C++ Users Journal, Dr. Dobb's Journal, MSDN Magazine, New Architect, SD Expo, SD Magazine, Sys Admin, The Perl Journal, UnixReview.com, Windows Developer Network