home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


  com.sun.java.swing.plaf.motif—The user interface classes for the Motif look and feel
  javax.swing.multi—The multiplexing user interface classes that enable you to make components from different factory classes
  com.sun.java.swing.plaf—Used by developers who want to write their own Pluggable Look and Feel (PLAF)
  javax.swing.table—The Swing Table class and its kin
  javax.swing.target—The support classes for Action target management
  javax.swing.text—Support for the Swing document framework
  javax.swing.undo—Support classes for implementing undo and redo
  javax.accessibility—Support for working with assistive technology to make Java programs accessible for people with disabilities


NOTE:  Swing is an emerging technology—not all Java-compatible browsers understand Swing, but most can be configured to run Swing applets. To test your browser (and to get instructions on reconfiguring your browser, if necessary) visit http://java.sun.com/products/jfc/swingdoc-current/applets.html.

Swing was first released as a separate set of classes that worked with JDK 1.1. Swing was not actually part of JDK 1.1, but had to be downloaded and installed separately. Swing remained a separate set of classes through much of the JDK 1.2 beta process. Now, however, Swing is fully integrated into JDK 1.2. Be sure to get the most up-to-date version of Swing by downloading the final version of Java 2/JDK 1.2 at http://java.sun.com/products/jdk/1.2/.


Before you begin to develop with Swing components, make sure you can run an existing Swing-based application. Sun includes SwingSet, an overview of the Swing components, in the JFC. You’ll find it in SWING_HOME/examples/SwingSet. Follow the instructions in the README.txt file in that directory to run the application. Make sure it loads and runs without errors.

Swing Component APIs

With the AWT you can choose buttons, labels, lists, and a few other user interface components. With the Swing components, Sun provides a richer interface, more akin to the full range available in Microsoft Foundation Classes or the Macintosh Toolbox. More than twice as many Swing components are available as AWT components. The Swing components include

  Labels
  Bordered panes
  Progress bars
  Tool tips
  Buttons
  Radio buttons
  Check boxes
  Tool bars
  Sliders
  Combo boxes
  Menus
  Trees
  Scroll bars
  List boxes
  Tabbed panes
  Tables

All these components are lightweight—instead of building a peer component from the native operating system, they look for a library of pluggable look and feel classes. Three such libraries come with Swing 1.0:

  SWING_HOME/windows.jar—A look and feel strongly resembling Windows 95/Windows NT 4.0.
  SWING_HOME/motif.jar—A look and feel based on Sun’s own Motif interface.
  SWING_HOME/metal.jar—A platform-independent look and feel.


NOTE:  Sun has announced a Macintosh look and feel
(http://java.sun.com/products/jfc/tsc/swingdoc-current/mac_l-f.html) that will be available in Swing 1.1. Swing 1.1 also includes a new version of the cross-platform Java look and feel, formerly known as Metal
(http://java.sun.com/products/jfc/tsc/swingdoc-current/plaf_report.html). Use the Java look and feel if you want your application or applet to look the same on every platform.

Most of Sun’s demo applets enable you to change the look and feel from a control at runtime. Figure 38.16 shows Simple, one of the examples, in the default look and feel (called Metal).


FIGURE 38.16  Assume that some users will want to switch from one look and feel to another.

Compare Figure 38.16 with Figure 38.17. Figure 38.16 shows Simple with the Metal look and feel. By choosing the Windows radio button, you can switch Simple to the Windows look and feel, shown in Figure 38.17. Similarly, you can use the Motif look and feel, shown in Figure 38.18.


FIGURE 38.17  Compare the Windows look and feel to Metal (shown in Figure 38.16) and Motif (in Figure 38.18).


FIGURE 38.18  The Motif look and feel is based on an interface Sun designed to its UNIX operating system.


NOTE:  Pluggable Look and Feel (PLAF) is here to stay. Use SwingSet to explore all three PLAFs, as well as others that will become available in the future. Remember that the decision about which look and feel to use is left to the user. Strive to make sure your design looks good in all available PLAFs.

Using the TPanelTester Application

The examples in this section are based on subclasses of the Swing panel component, JPanel. To run the various panels, you need an application to display the panel. Listing 38.7 shows a generic panel tester.

Listing 38.7 TPanelTester.javaChange the Name in This Panel Tester to Match the Panel Under Test


import javax.swing.*; 

public class TPanelTester extends JFrame {
  public TPanelTester() {
    super(“Panel Tester”);

    // change TLabelPanel to match the name of the panel under test
    JPanel thePanelUnderTest = new TLabelPanel();
    setContentPane(thePanelUnderTest);
  }
    
  public static void main(String[] args) {
    JFrame theFrame = new TPanelTester();
    theFrame.addWindowListener(new java.awt.event.WindowAdapter() {
      public void windowClosing(java.awt.event.WindowEvent e) 
        {System.exit(0);}
      });
    theFrame.pack();
    theFrame.setVisible(true);
  }
}

Using JPanel

We first looked at panels earlier in this chapter in our discussion of the AWT. A JPanel is a lightweight version of panel; it is used in most of the examples in this section.


NOTE:  In Chapter 39, “Graphics and Animation,” we’ll talk about how you can reduce flicker with double buffering. Double buffering is built into JComponent; check the documentation for the setDoubleBuffered() method.

Working with Icons

In our discussion of the AWT, we noted that subclasses of java.awt.Container can contain java.awt.Components, and that Containers themselves are Components. It is this hierarchy that enables us to add Panels to Frames, for example. All the Swing components are derived from JComponent, which is a java.awt.Container. This design means that every JComponent can contain other components, either AWT or Swing. Therefore, you can add a graphical icon to a JButton, a JLabel, or other Swing component. Swing provides Icon as an interface; to implement it you must provide a paintIcon() method, a getIconWidth() method, and a getIconHeight() method. The paintIcon() method is

paintIcon(Component c, Graphics g, int x, int y);


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.