How This Book Is Organized

Java > Core SWING advanced programming > Preface > How This Book Is Organized

 

The first five chapters of this book deal almost exclusively with the Swing text components. Although superficially simple, these components are built on a complex and flexible software architecture that makes it possible to customize or extend them to accomplish almost any task. However, since there is little documentation for these components available in the JDK, the main problem that most developers encounter is working out how to exploit the text controls to the fullest.

Chapter 1 deals with the simple JTextField and JPasswordField components. In many applications, the user will interact almost exclusively with these single-line controls. If your application has a predominantly form-based interface, you'll need to know exactly how to customize these two controls to make it easy for the user to complete forms and to minimize the possibility of incorrect information being entered. The chapter begins with an overview of the text package and shows the architecture that underlies all of the text controls, much of which will be familiar if you have read Core Java Foundation Classes or a similar introductory Swing book. The rest of the chapter shows you how to create specialized text controls, including a text field that only allows the user to type valid numeric values, and another one that tracks what the user types and "autocompletes" the field from a list of legal values so that the user may only need to type a small number of characters to select the input value required.

Chapter 2 looks at how the text package deals with more complex text that can mix multiple fonts, colors, and other styles. This chapter shows you how to use the Swing JTextPane control to display programmatically-created text and how to control the layout of lines and paragraphs on the screen.

Chapter 3 builds on the material covered in the first two chapters by showing you how to change the appearance of the text in a text control without changing the way in which the data is represented internally. This is possible because the Swing text components rely on classes called "views" to draw their content onto the screen. Views use the text stored in the text control plus attributes supplied along with it to determine what to draw. This chapter shows you how to create custom views that use existing or private attributes to display the same data in various different ways. A popular application of this technique, and the central example of this chapter, is the ability to create a text field that adds formatting to the text that it contains, without the formatting being held in the data itself. You might use this feature to have a series of digits displayed as a telephone number, such as

 (123) -456-7890 

without needing to have the parentheses and the hyphens store in the text field itself, the advantage of which is that the application does not need to have code to remove them when it reads the telephone number from the text field.

You'll also see how to use private attributes with JTextPane to add extra facilities, such as a paragraph with a colored background or wrapped with a custom border.

Chapter 4 is probably the most interesting of the text-related chapters. This chapter deals with the JEditorPane control, which can be used to render and edit files that contain plain text, HTML, or encoded Rich Text Format (RTF). You'll see how to load the text into the JEditorPane, how to write out modifications, how to use it to display Web pages held locally or on the Internet, and how to implement the correct behavior for hypertext links. Much of this material has been covered, to some degree, in other Swing books, even some of the most basic ones. Here, though, it is just the introduction to the second part of the chapter, which contains a detailed examination of the Swing HTML package. Using the material in this chapter, you'll be able to load HTML into JEditorPane much faster than you can if you use the standard JEditorPane setPage method. You'll see how to use the HTML package to load and analyze an HTML page without displaying it to the user and to extract information from it, such as a list of the links to other pages that it contains. You'll also see the internal data structures used by JEditorpane when it is displaying HTML and how to programmatically modify them to change the way in which the content is displayed.

Chapter 5 describes the built-in support for bi-directional text, an internationalization feature which allows text in languages such as Arabic that are predominantly read from right to left to be rendered properly when displayed on the screen. Although you can use this facility without really being aware of it (in the sense that the text components know when text should be rendered right to left without intervention by the developer), there are several subtle points that you need to understand if you are developing an application that might be used in an environment in which the reading order is not the usual left to right that you are probably most familiar with. Chapter 5 covers these points by showing you how the text components handle bi-directional text and how their internal structure is affected by it.

Chapters 6 and 7 cover JTable rendering and editing, respectively. These topics probably cause more confusion for developers than any other aspect of the Swing API, even though (and, in fact, probably because) the underlying concepts are fairly simple. Chapter 6 begins by covering the rendering mechanism, then proceeds to show how you can customize it to add special effects to an entire table, to a specific row or rows, or to a single cell. You'll also see how to make use of tooltips to present more detailed information to the user. Chapter 7 covers the related topic of in-cell editing with JTable. This feature is almost certainly one of the least understood parts of the JTable API, judging by the number of times it crops up in the newsgroups. The first part of Chapter 7 confronts the major point of confusion straight away-the fact that the editing component does not permanently reside in the table, but appears only when it is required-by describing in detail the interaction between the table, the mouse and keyboard and the editor itself. This discussion is illustrated by combining editing and rendering techniques to give the impression that a table has a button in each row. The button looks and behaves like a real button, even though it is not actually present most of the time. In this chapter, you'll also see how to take control of the mechanics of the editing process and make it easier for a user to quickly update data in a table by adding support for tabbing between editable columns using the TAB key, a feature that is not available by default. The same technique can, of course, be used to add other usability features to the table.

Chapter 8 describes the Java 2 drag-and-drop feature and is the only part of this book that is not applicable if you are going to be using JDK 1.1 for the foreseeable future. Drag-and-drop is not part of the Swing package, but it does come under the broader heading of the Java Foundation Classes. By adding drag-and-drop support to your application, you can make it much easier for the user to copy or move data (in whatever form) between different parts of your application or even to or from a different one. This chapter starts by outlining the basic architecture of the drag-and-drop subsystem and then shows you how to implement both the "drag source" and the "drop target" for different types of information, including text and graphical components. Finally, you'll see a complete implementation of what is probably the most frequently discussed drag-and-drop related facility-a tree that supports moving or copying of data by dragging nodes, which represent the data, with the mouse and dropping them on the target node. The best feature of this example is that the source and target of the operation don't have to be in the same application and, in fact, it works even if the source or target of the operation is a native (non-Java) application.

One of the more unusual features of Swing is a complete package dedicated to giving the user undo and redo operations performed from the user interface. The code in this package is leveraged by the Swing text components to allow developers to roll back changes made in simple input fields or even in the more complex JTextPane and JEditorPane where a user might modify not only content but also fonts, colors, and text alignment. Although the underlying support for these operations is present in the text controls, you still have to do some work to make it available to the user. Chapter 9 shows you not only how to do this, it also shows the architecture of the Undo package, how to use the undo mechanism in other situations which have nothing to do with text handling, and how to extend it to add some useful features that are not available straight out of the box.

 

 



Core Swing
Core Swing: Advanced Programming
ISBN: 0130832928
EAN: 2147483647
Year: 1999
Pages: 55
Authors: Kim Topley

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net