epubjs
Version:
Render ePub documents in the browser, across many devices
81 lines (80 loc) • 8.95 kB
HTML
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Chapter 16. Swing</title><link rel="stylesheet" href="core.css" type="text/css"/><meta name="generator" content="DocBook XSL Stylesheets V1.74.0"/></head><body><div class="chapter" title="Chapter 16. Swing"><div class="titlepage"><div><div><h1 class="title"><a id="learnjava3-CHP-16"/>Chapter 16. Swing</h1></div></div></div><p>Swing is Java’s graphical user interface toolkit. The <code class="literal">javax.swing</code> package (and its numerous subpackages)
contain classes representing interface items such as windows, buttons, combo
boxes, trees, tables, and menus—everything you need to build a modern, rich
client-side application.</p><p><a id="I_indexterm16_id784703" class="indexterm"/> <a id="I_indexterm16_id784710" class="indexterm"/>Swing is part of a larger collection of software called the
Java Foundation Classes (JFC), which includes the following APIs:</p><div class="itemizedlist"><ul class="itemizedlist"><li class="listitem"><p><a id="I_indexterm16_id784721" class="indexterm"/> <a id="I_indexterm16_id784727" class="indexterm"/>The Abstract Window Toolkit (AWT), the original user
interface toolkit and base graphics classes</p></li><li class="listitem"><p>Swing, the pure Java user interface toolkit</p></li><li class="listitem"><p>Accessibility, which provides tools for integrating nonstandard
input and output devices into your user interfaces</p></li><li class="listitem"><p>The 2D API, a comprehensive set of classes for high-quality
drawing</p></li><li class="listitem"><p>Drag and Drop, an API that supports the drag-and-drop
metaphor</p></li></ul></div><p>JFC is one of the largest and most complex parts of the standard Java
platform, so it shouldn’t be any surprise that we’ll take several chapters
to discuss it. In fact, we won’t even get to talk about all of it, just the
most important parts—Swing and the 2D API. Here’s the lay of the
land:</p><div class="itemizedlist"><ul class="itemizedlist"><li class="listitem"><p>This chapter covers the basic concepts you need to understand how
to build user interfaces with Swing.</p></li><li class="listitem"><p><a class="xref" href="ch17.html" title="Chapter 17. Using Swing Components">Chapter 17</a> discusses the basic
components from which user interfaces are built: buttons, lists, text
fields, checkboxes, and so on.</p></li><li class="listitem"><p><a class="xref" href="ch18.html" title="Chapter 18. More Swing Components">Chapter 18</a> dives further into the Swing
toolkit, describing text components, trees, tables, and other advanced
components.</p></li><li class="listitem"><p><a class="xref" href="ch19.html" title="Chapter 19. Layout Managers">Chapter 19</a> discusses layout managers,
which are responsible for arranging components within a window.</p></li><li class="listitem"><p><a class="xref" href="ch20.html" title="Chapter 20. Drawing with the 2D API">Chapter 20</a> discusses the fundamentals of
drawing, including simple image display.</p></li><li class="listitem"><p><a class="xref" href="ch21.html" title="Chapter 21. Working with Images and Other Media">Chapter 21</a> covers the image generation
and processing tools of the <code class="literal">java.awt.image</code> package. We’ll throw in audio
and video for good measure.</p></li></ul></div><p>We can’t cover the full functionality of Swing in this book; if you
want the whole story, see <span class="emphasis"><em><a class="ulink" href="http://shop.oreilly.com/product/9780596004088.do">Java
Swing</a></em></span> by Marc Loy, Robert Eckstein, Dave Wood, Brian
Cole, and James Elliott (O’Reilly). Instead, we’ll cover the basic tools you
are most likely to use and show some examples of what can be done with some
of the more advanced features. <a class="xref" href="ch16.html#learnjava3-CHP-16-FIG-1" title="Figure 16-1. User interface components in the javax.swing package">Figure 16-1</a>
shows the user interface component classes of the <a id="I_indexterm16_id784829" class="indexterm"/><code class="literal">javax.swing</code> package.</p><p>To understand Swing, it helps to understand its predecessor, AWT. As
its name suggests, AWT is an abstraction. Like the rest of Java, it was
designed to be portable; its functionality is the same for all Java
implementations. However, people generally expect their applications to have
a consistent look and feel and that is usually different on different
platforms. So AWT was designed to provide the same functionality on all
platforms, yet have the appearance of a native application. The idea is that
you could choose to write your code under Windows, then run it on an X
Window System or a Macintosh and get more or less the native look and feel
on each platform for free.To achieve platform binding, AWT uses
interchangeable <span class="emphasis"><em>toolkits</em></span> that interact with the host
windowing system to display user interface components. This shields the Java
application from the details of its environment in which it’s running and
keeps the APIs pure. Let’s say you ask AWT to create a button. When your
application or applet runs, a toolkit appropriate to the host environment
renders the button appropriately: on Windows, you can get a button that
looks like other Windows buttons; on a Macintosh, you can get a Mac button;
and so on.</p><p>AWT had some serious shortcomings, however. The worst was that the use
of platform-specific toolkits meant that AWT applications might be subtly
incompatible on different platforms. Furthermore, AWT lacked advanced user
interface components, such as trees and grids, which were not common to all
environments. AWT provided the desired look and feel, but limited the
features and true portability of Java GUI applications.</p><p>Swing takes a fundamentally different approach. Instead of using
native toolkits to supply interface items, such as buttons and combo boxes,
components in Swing are implemented in Java itself. This means that,
whatever platform you’re using, by default a Swing button (for example)
looks the same. However, Swing also provides a powerful, pluggable
look-and-feel API that allows the native operating system appearance to be
rendered at the Java level. Working purely in Java makes Swing much less
prone to platform-specific bugs, which were a problem for AWT. It also means
that Swing components are much more flexible and can be extended and
modified in your applications in ways that native components could never
be.</p><div class="figure"><a id="learnjava3-CHP-16-FIG-1"/><div class="figure-contents"><div class="mediaobject"><a id="I_16_tt976"/><img src="httpatomoreillycomsourceoreillyimages1707648.png" alt="User interface components in the javax.swing package"/></div></div><p class="title">Figure 16-1. User interface components in the javax.swing package</p></div><p>Working with user interface components in Swing is meant to be easy.
When building a user interface for your application, you’ll be working with
a large set of prefabricated components. It’s easy to assemble a collection
of user interface components (buttons, text areas, etc.) and arrange them
inside containers to build complex layouts. However, when necessary, you can
build upon these simple components to make entirely new kinds of interface
gadgets that are completely portable and reusable.</p><p><a id="I_indexterm16_id784907" class="indexterm"/>Swing uses <span class="emphasis"><em>layout managers</em></span> to arrange
<span class="emphasis"><em>components</em></span> inside <a id="I_indexterm16_id784919" class="indexterm"/><span class="emphasis"><em>containers</em></span> and control their sizing and
positioning. Layout managers define a strategy for arranging components
instead of specifying absolute positions. For example, you can define a user
interface with a collection of buttons and text areas and be reasonably
confident that it will always display correctly, even if the user resizes
the application window. It doesn’t matter what platform or user interface
look-and-feel you’re using; the layout manager should still position them
sensibly with respect to each other.</p><p>The next two chapters contain examples using most of the components in
the <code class="literal">javax.swing</code> package. Before we dive
into those examples, we need to spend some time talking about the concepts
Swing uses for creating and handling user interfaces. This material should
get you up to speed on GUI concepts and how they are used in Java.</p></div></body></html>