Menu
Swing provides many new features for thoseplanning to write large-scale applications in Java. Here is anoverview of some of the more popular features.
4.) Which of the following statements about GUI components is wrong? Options are: A.) Swing exists since version 1.2 of the jdk: B.) You can not place AWT components on Swing containers: C.) AWT stands for Abstract Window Toolkit: D.) The AWT classes are deprecated.
Oneof the most exciting aspects of the Swing classes is the ability todictate the look-and-feel (L&F) of each of thecomponents, even resetting the look-and-feel at runtime.Look-and-feels have become an important issue in GUI development overthe past five years. Most users are familiar with the Motif style ofuser interface, which was common in Windows 3.1 and is still in wideuse on Unix platforms. Microsoft has since deviated from thatstandard with a much more optimized look-and-feel in their Windows95/98 and NT 4.0 operating systems. In addition, the Macintoshcomputer system has its own branded look-and-feel, which most Appleusers feel comfortable with.
Swing is capable of emulating several look-and-feels, and currentlyincludes support for Windows 98 and Unix Motif.[4] This comes in handy when a user would like to work in theL&F environment which he or she is most comfortable with. Inaddition, Swing can allow the user to switch look-and-feels atruntime without having to close the current application. This way, auser can experiment to see which L&F is best for them withinstantaneous feedback. And, if you’re feeling really ambitiousas a developer (perhaps a game developer), you can create your ownlook-and-feel for each one of the Swing components!
Swingcomes with a default look-and-feel called “Metal,” whichwas developed while the Swing classes were in the beta-release phase.This look-and-feel combines some of the best graphical elements intoday’s L&Fs and even adds a few surprises of its own.Figure 1.3 shows an example of severallook-and-feels that you can use with Swing, including the new Metallook-and-feel. All Swing L&Fs are built from aset of base classes called the Basic L&F. However, though we mayrefer to the Basic L&F from time to time, you can’t use iton its own.
Figure 1-3. Various look-and-feels in the Java Swing environment
Most Swing components arelightweight . In the purest sense, this means thatcomponents are not dependent on native peers to render themselves.Instead, they use simplified graphics primitives to paint themselveson the screen and can even allow portions to be transparent.
The ability to create lightweight components first emerged in JDK1.1, although the majority of AWT components did not take advantageof it. Prior to that, Java programmers had no choice but to subclass
java.awt.Canvas
orjava.awt.Panel
if they wished to create their owncomponents. With both classes, Java allocated an opaque peer objectfrom the underlying operating system to represent the component,forcing each component to behave as if it were its own window, takingon a rectangular, solid shape. Hence, these components earned thename “heavyweight,” because they frequently held extrabaggage at the native level that Java did not use.Heavyweight components were unwieldyfor two reasons:
- Equivalent components on different platforms don’t necessarilyact alike. A list component on one platform, for example, may workdifferently than a list component on another. Trying to coordinateand manage the differences between components was a formidable task.
- The look-and-feel of each component was tied to the host operatingsystem and could not be changed.
With lightweight components, each component renders itself using thedrawing primitives of the
Graphics
object (e.g.,drawLine()
, fillRect()
, etc.).Lightweight components always render themselves onto the surface ofthe heavyweight top-level component they are contained in. With thearrival of JDK 1.1, programmers can directly extend thejava.awt.Component
orjava.awt.Con-tainer
classes when creatinglightweight components. Unlike java.awt.Canvas
orjava.awt.Panel
, these classes do not depend on anative peer and allow the developer to render quickly to the graphicscontext of the container. This results in faster, lessmemory-intensive components than were previously available in Java.Almost all of the Swing components are lightweight; only a fewtop-level containers are not. This design allows programmers to draw(and redraw) the look-and-feel of their application at runtime,instead of tying it to the L&F of the host operating system. Inaddition, the design of the Swing components supports easymodification of component behavior. For example, you can indicate toalmost any Swing component whether you wish it to accept or declinefocus, and how it should handle keyboard input.
Several other features distinguish Swing from the older AWTcomponents:
- A wide variety of new components, such as tables, trees, sliders,progress bars, internal frames, and text components.
- Swing components contain support for replacing their insets with anarbitrary number of concentric borders.
- Swing components can have tooltips placed overthem. A tooltip is a textual popup that momentarily appears when themouse cursor rests inside the component’s painting region.Tooltips can be used to give more information about the component inquestion.
- You can arbitrarily bind keyboard events to components, defining howthey will react to various keystrokes under given conditions.
- There is additional debugging support for the rendering of your ownlightweight Swing components.
We will discuss each of these features in greater detail as we movethrough the next three chapters.
Not everyone will use Swing for the samereasons. In fact, the Swing libraries have many levels of use, eachwith their own level of prerequisite knowledge. Here are somepotential uses:
- Use the Swing components as they are to build your own enterpriseapplications.
- Create your own Swing components—or extend those that alreadyexist.
- Override or create a new look-and-feel for one or more of the Swingcomponents.
![Components Components](/uploads/1/2/6/3/126303884/424831737.png)
The first approach is what the vast majority of Swing programmerswill use. Here, using Swing components is just like using the AWTcomponents. A familiar set of components, containers, and layoutmanagers are all available in the Swing packages to help you get yourapplication up and running quickly. If you’re adept at AWTprogramming, you will probably need only a cursory introduction toeach component to get started. Only in the event of some of thelarger and newer component families, such as tables and text, will weneed to get into broader issues. If you are planning to use eachcomponent as a Java Bean for visual programming, you will also fallinto this category.
Creating your own component, or extending an already existing one,requires a deeper understanding of Swing. This includes a firmunderstanding of Swing architecture, events, and lower-level classes.Also, if you decide to subclass a Swing component, theresponsibilities of that component must be adopted and handledaccordingly—otherwise, your new component may performerratically.
Finally, you may wish to change the look-and-feel of one or moreSwing components. This is arguably the most complex of the threeroutes that you can take—it requires a thorough knowledge ofthe design, architectural fundamentals, and graphical primitives ofeach lightweight component. In addition, you will need to understandhow Swing’s
UIManager
andUIDefaults
classes work together to“set” each component’s look-and-feel.This book strives to help you with each of these issues. Because weanticipate that the vast majority of readers will fall under thefirst category, we spend a great deal of time reviewing eachcomponent’s properties and methods, as well as providing sourcecode for various scenarios that use these components. We alsodocument the protected methods and fields. Programmers can use theseto extend the Swing components into their own master creations.
Programming your own look-and-feel can get pretty complex; in fact,the source code for an entire look-and-feel would far exceed the sizeof even this book. However, we don’t want to leave you in thedark. If you are an experienced Swing programmer already, andyou’re looking for a concise introduction on how to getstarted, see Chapter 26. This chapter provides someexcellent examples of how to code your own look-and-feel for bothsimple and complex Swing components.com.sun.java.accessibility
[4] Anearly access version of the Macintosh look-and-feel has beenreleased. For more information see http://developer.java.sun.com/developer/earlyAccess/jfc/.
- SWING Tutorial
- SWING Useful Resources
- Selected Reading
Swing API is a set of extensible GUI Components to ease the developer's life to create JAVA based Front End/GUI Applications. It is build on top of AWT API and acts as a replacement of AWT API, since it has almost every control corresponding to AWT controls. Swing component follows a Model-View-Controller architecture to fulfill the following criterias.
- A single API is to be sufficient to support multiple look and feel.
- API is to be model driven so that the highest level API is not required to have data.
- API is to use the Java Bean model so that Builder Tools and IDE can provide better services to the developers for use.
MVC Architecture
Swing API architecture follows loosely based MVC architecture in the following manner.
- Model represents component's data.
- View represents visual representation of the component's data.
- Controller takes the input from the user on the view and reflects the changes in Component's data.
- Swing component has Model as a seperate element, while the View and Controller part are clubbed in the User Interface elements. Because of which, Swing has a pluggable look-and-feel architecture.
Swing Features
- Light Weight − Swing components are independent of native Operating System's API as Swing API controls are rendered mostly using pure JAVA code instead of underlying operating system calls.
- Rich Controls − Swing provides a rich set of advanced controls like Tree, TabbedPane, slider, colorpicker, and table controls.
- Highly Customizable − Swing controls can be customized in a very easy way as visual apperance is independent of internal representation.
- Pluggable look-and-feel − SWING based GUI Application look and feel can be changed at run-time, based on available values.