February 27, 2021
Hot Topics:

Implications of Swing's pluggable look and feel

  • By Ted M. Young
  • Send Email »
  • More Articles »

February 12, 1998
Implications of Swing's pluggable look and feel

by Ted M. Young

The idea of a "pluggable look and feel," or PLaF for short, plays a prominent role in the Swing set of widgets (user-interface components), in JavaSoft's Java Foundation Classes (JFC) release in JDK 1.2. A "look and feel" (LaF) refer to components' appearance (grey vs. white, flat vs. three-dimensional, etc.) and their interactions with users (how they react to keypresses, mouseclicks and the features they offer, such as floating toolbars). A PLaF means that you can write your program using a particular look-and-feel, for example the Windows 95 LaF, and then at runtime you can switch to another LaF, such as Motif, or the new "Organic" LaF (developed specifically for use in Java), or even a pen-based LaF for palm computers. This saves developers the trouble of changing user-interface code.

However, this advantage has some ramifications that you need to be aware of. In particular, you can't always assume that a particular LaF offers the features that you want. An example (prompted by a question in the JavaGUI section of developer.com's Discussions area) is found in the dockable toolbar and, in particular, the ability to programatically control whether the toolbar is docked or floating. As of Swing 0.7.1, the JToolBar component (the Swing toolbar) does not offer methods to support the control of a dockable toolbar, even though the Windows LaF and the Java LaF both support dockable toolbars (alas, the dockable feature is somewhat buggy in this release of Swing).

So what to do? The answer is to use the code included with this article, which shows how to access methods in the underlying user interface (i.e., the specific LaF being used). This demo application creates a window with three buttons (ok, four, but the first one is just for show): float, unfloat and exit. When you click on the Float button, the toolbar floats in its own top-level window that you can drag around the screen. When you click on the Unfloat button, the toolbar returns to its home frame. I'll leave the effect of the Exit button to your imagination.

These tasks are accomplished with the

method (this method gets called when you click on the button):

First, we get a reference to the underlying user-interface object, in this case a


ToolBarUI tbUI = m_tb.getUI(); // m_tb is a reference to our JToolBar instance

Then we see if this particular UI object is a

, because we know that
s support floating toolbars:
if ( tbUI instanceof BasicToolBarUI ) {

Then, if it is a

, we can access the
's methods for controlling the floating aspect of the toolbar. First, we cast the tbUI reference to
so we can invoke the
method (we'll float the toolbar at coordinates 50,50 on the screen):
( ( BasicToolBarUI ) tbUI).setFloatingLocation( 50, 50 );

Then we'll actually force the toolbar to float by invoking

, again casting to a
( ( BasicToolBarUI ) tbUI).setFloating( true, null );

And, since there are some painting bugs in Swing, we force some repaints:


To Unfloat, we simply use the same method to invoke
with a false parameter to stop floating.

And that's it! Keep in mind that this fix is specific to

types. We could check for other types, but any other LaF implementation would be specific to that LaF.

There are other

-specific methods that we can invoke in the same manner. Take a look at the Swing documentation in the com.java.swing.basic package for more.

Note that JavaSoft may change the JToolBar implementation and "pull" the floating functionality from the underlying UI, thereby forcing all LaFs to support floating toolbars, which would make this example moot. However, there will always be times when you need to access specific features of the underlying LaF user-interface implementation.

See the code mentioned in this article.

Join Ted for more Java talk in the developer.com Discussions area.

Ted M. Young, CTO and co-founder of Advanced Web Technologies Corp., a Java training and mentoring company, has been busy transferring his Java knowledge to developers since 1995 through hands-on training, mentoring, and writing. Besides Java, his specialties include object-oriented design and architecture and user interface design. You can write him at: tyoung@javatrain.com.

Page 1 of 2

This article was originally published on February 12, 1998

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date