You are previewing Core SWING advanced programming.
O'Reilly logo
Core SWING advanced programming

Book Description


The experienced developer's guide to the Swing classes!

Core Swing helps you deliver the sophisticated Java GUI applications your users demand—with Java Swing techniques that start where other books leave off! Building on his bestseller Core Java Foundation Classes, Kim Topley takes on the toughest Swing issues raised by programmers throughout the Java community and addresses crucial topics first-generation Swing books completely missed.

With Core Swing, you'll discover how to exploit the Swing tree and table controls to the fullest, customize and create your own text fields with features that sophisticated users expect, build your own editors and file viewers, and much more. There is complete and in-depth coverage of Swing's HTML support, a topic barely addressed in other Swing books. You'll also find detailed coverage of undo/redo and drag-and-drop, with lengthy, fully documented code examples, in the book and on the accompanying CD-ROM.

Every Core Series book:

  • DEMONSTRATES how to write commercial quality code

  • FEATURES dozens of nontrivial programs and examples—no toy code!

  • FOCUSES on the features and functions most important to real developers

  • PROVIDES objective, unbiased coverage of cutting-edge technologies—no hype!

  • Core Swing: Advanced Programming delivers:

  • State-of-the-art insights into the Swing classes that other books can't touch

  • Practical techniques for building the Swing components you really need

  • Fast-track techniques for making the most of text controls, editors, tables, and drag-and-drop

  • Hundreds of professional-level code examples

  • CD-ROM

    The accompanying CD-ROM includes all of the book's detailed code examples a bonus chapter taken from Kim Topley's first book, Core Java Foundation Classes, and trial versions of SwingBuilder from SwingSoft and Netbeans.

    Table of Contents

    1. Copyright
      1. Dedication
    2. Preface
      1. Who This Book Is For
      2. What You'll Need
      3. How This Book Is Organized
      4. Conventions Used in This Book
      5. About the CD-ROM
        1. The Example Source Code
          1. Extracting the Example Source Code
          2. Compiling the Examples
          3. Running the Examples
          4. The COREJFC Directory
          5. The SOFTWARE Directory
            1. NetBeans
            2. SwingBuilder
      6. Feedback
      7. Further Information
      8. Acknowledgments
      1. The Swing Text Components
        1. The JTextField and JPasswordField Components
          1. Creating a Text Field
          2. Text Field Size
          3. Content Alignment
          4. Scroll Offset
        2. The JTextArea Control
          1. Text Area Size, Scrolling, and Wrapping
        3. JEditorPane and JTextPane
        4. Common Text Component Features
          1. Editable and Enabled States
          2. Focus Accelerator Keys
          3. Selection and Data Transfer
          4. The User's View of Text Component Cut-and-Paste Operations
          5. The Programmer's View
        5. Text Components, Text Wrapping, and Scrolling
          1. Controlling Line and Word Wrapping
          2. Program-Controlled Scrolling of Text Components
      2. Adding Functionality to the Basic Text Components
        1. Text Component Architecture
          1. The Document
          2. The Highlighter
          3. The Caret
          4. The Editor Kit
        2. Storing Document Content
          1. The Document Interface
            1. Insertion and Removal of Text
          2. Retrieving Model Content
            1. Positions
            2. Properties
            3. Event Handling
            4. Elements and Rendering
        3. Implementing Text Fields with Input Validation
          1. A Text Field with Limited Input Capacity
            1. Limiting Input using a DocumentListener
            2. Listening to Keyboard Input
          2. Using the Document Model
          3. Creating a Numeric Input Control
            1. Checking for Valid Numbers
            2. Implementing the Model—Outline
          4. Implementing the Model—The insertString Method
            1. Retrieving the Model Value
            2. Implementing the Numeric Text Field
        4. A Text Field with Look-Ahead
        5. Text Actions and Keyboard Mappings
          1. Text Actions
          2. Keymaps and Key Bindings
            1. The KeyBinding and KeyMap Classes
            2. Creating Individual Keymaps
            3. Creating Keymaps for Text Components
            4. Resolving Key Bindings
            5. The Default Keyboard Action
          3. Disabling the RETURN Key in a Text Field
            1. Removing the RETURN key from the Keymap
            2. Creating a Replacement Keymap
            3. Another Method—Intercepting the RETURN key
          4. Implementing Overwrite Mode in a Text Field
            1. Implementing the Overwrite Capability
            2. Switching Between Insert and Overwrite Modes
            3. Switching the Cursor
            4. Implementing a Custom Caret
      3. Summary
      1. Documents with Attributes
        1. Attributes, Styles, and Style Contexts
          1. Attributes, Attribute Sets, and Styles
            1. Attribute Sets
            2. Mutable Attribute Sets
            3. Styles and StyleContexts
          2. Inserting Text into a JTextPane
            1. Creating StyleContexts
            2. Adding Styles to a StyleContext
            3. Type-Safe Attribute Handling with StyleConstants
          3. Paragraph Attributes, Character Attributes, and Logical Styles
            1. Attribute Hierarchy
            2. More on Attribute Hierarchy
            3. The Default Style
            4. A Simpler Way to Apply Attributes
            5. Inserting Icons and Components
      2. Summary
      1. Customized Document Views
        1. How Attributes Are Stored
          1. The Element Structure of the Simple Text Components
          2. Elements and Element Types
          3. Elements and JTextPane
        2. Views: Rendering the Document and Its Attributes
          1. The Basic Views
            1. Views and the View Hierarchy
            2. The View Class
            3. View Factories
          2. The View Hierarchies of JTextField and JTextArea
            1. Looking at the View Hierarchy
            2. Views and JTextField
            3. JTextArea: Nonwrapping and Wrapping Views
          3. The Views Used with JEditorPane and JTextPane
            1. A Typical JTextPane View Hierarchy
            2. Paragraph Size, Line Layout, and Wrapping
            3. A Few Minor Details
          4. Handling Tabs
            1. Specifying Tab Positions
            2. Views and Tab Expansion
            3. Handling Embedded Icons and Components
          5. A Customized Paragraph View
            1. Anatomy of the Extended ParagraphView
            2. Specifying the Border and the Background Color
            3. A New ViewFactory
            4. Implementing the New Paragraphview
            5. Setting the Paragraph Insets
            6. Painting the Paragraph
            7. Using the ExtendedParagraphView
      2. Highlighting and Highlighters
        1. The Highlighter and Highlight Painter Interfaces
        2. The LayeredHighlighter Class
        3. Creating a Custom Highlighter
          1. An Underlining Highlighter
            1. Implementing the UnderlineHighlighter
            2. The UnderliningHighlightPainter
          2. Using the Highlighter
      3. Custom Views and Input Fields
        1. The Formatted Text Field
          1. The ViewFactory
          2. A Custom FieldView
            1. Initialization and State
            2. Determining the Preferred Span of the Text Field
            3. Mapping from View Location to Model Position
            4. Mapping from the Model to the View
            5. Drawing the Field Content—Overview
            6. Implementing the drawLine Method
      4. Summary
      1. The JEditorPane Control
        1. A Simple HTML Viewer
        2. Initializing a JEditorPane
        3. Content Type and Editor Kit Selection
          1. Installation of an Editor Kit
          2. The setContentType Method
          3. Configuration from the Input Source
        4. Loading Document Content
          1. Document Loading
        5. Asynchronous Page Loading
        6. Character Set Handling
        7. Loading RTF Documents
      2. Saving Document Content
        1. Document Output Format
        2. Saving RTF Files
        3. Document Type Conversion
      3. The Swing HTML Package
        1. The HTML Document Model
          1. The DTD
          2. The Parser and the ParserCallback
            2. Start and End Tags
            3. Simple Tags
            4. Text
            5. Errors
            6. The flush Method
            7. The Default Parser
            8. Replacing the ParserCallback
          3. The HTMLDocument class
            1. The Structure of an HTMLDocument
            2. Supported HTML Tags and Attributes
          4. Loading Content into an HTMLDocument
            1. A Class That Loads HTML
            2. Loading Web Pages with and without JEditorPane
          5. Using HTML/Document to Analyze HTML
            1. Searching for Hypertext Links
            2. Another Way to Scan for Tags
            3. Building a Hierarchy of Document Headings
          6. Hypertext Links
        2. Style Sheets and HTML Views
          1. Style Sheets
            1. Style Sheet Overview
            2. HTML Attributes and View Attributes
          2. Changing an HTML Document's Style Sheet
            1. The Style Sheet Hierarchy
            2. Installing a New Default StyleSheet
            3. Making Changes to the Default StyleSheet
            4. Changing the StyleSheet for Individual Documents
          3. The HTML Views
            1. Creating a Custom View
        3. The HTML Editor Kit
          1. Using the HTML Editor Kit Text and HTML Actions
            1. Constructing Menus from Editor Kit Actions
            2. Using Editor Kit Actions
            3. Creating the Application Menu Bar
            4. Using HTML Actions
          2. Adding Custom HTML Actions
            1. Creating New HTML Actions
            2. Returning New Actions from the getActions Method
      4. Summary
      1. Bi-Directional Text
        1. Model Order and Visual Order
        2. Caret Position and the Selection
        3. How Bi-Directional Text Is Handled in the Swing Text Components
          1. Representation of Bi-Directional Text within AbstractDocument
          2. Rendering Bi-Directional Text
          3. Views and Position Bias
          4. The Caret
          5. The Selection
      2. Summary
      1. Table Rendering
        1. Rendering Basics
          1. Selecting the Renderer for a Column
          2. A Simple Rendering Example— the Currency Table
        2. Implementing a Custom Renderer
          1. How the Default Renderers Work
          2. Creating a Renderer That Displays Decimal Numbers
          3. A Renderer for Text and Icons
          4. Calculating the Width of a Table Column
        3. Customizing Cells and Rows
          1. A Row-Based Renderer: The Striped Table
          2. Cell-Based Rendering
        4. Header Rendering
          1. A Multi-Line Column Header Renderer
          2. A Multi-Line Cell Renderer
        5. Renderers and Tooltips
      2. Summary
      1. Overview of the Table Editing Mechanism
        1. An Editable Currency Table
          1. Implementing an Editable Table Model
      2. The Table Editing Process— How It Works
        1. Selecting an Editor
        2. The TableCellEditor and CellEditor Interfaces
        3. The DefaultCellEditor Class
        4. The Mechanics of the Editing Process
          1. Starting the Edit
            1. Beginning an Edit on Mouse Action
            2. Beginning an Edit Using the Keyboard
            3. Explicitly Starting an Edit from Application Code
          2. Editing the Cell Data
          3. Ending the Edit
      3. Using Table Editors
        1. A Table with a Combo Box Editor
        2. Including Buttons in a Table
          1. Adding the Update Button Column
          2. The Button Renderer
          3. Activating the Button
            1. Creating a Custom Editor Base Class
            2. Extending the Base Class to Create a Button Editor
      4. Tabbing Between Editable Cells
        1. Designing the Edit Traversal Mechanism
        2. Implementing a Table with TAB Edit Traversal
          1. Specifying the Table Columns
          2. Changing the Editing Cell
      5. Summary
    10. 8. DRAG-AND-DROP
      1. Drag-and-Drop Overview
        1. The Drag Source
        2. The Drop Target
        3. Data Transfer
      2. Architecture of the Java Drag-and-Drop Subsystem
        1. Drag Source Architecture
        2. Drop Target Architecture
        3. Transferring Data
      3. Implementing a Drop Target
        1. A Simple Drop Target
          1. Creating and Using the DropTarget
          2. The dragEnter Method
          3. The dragOver, dragExit, and dropActionChanged Methods
          4. Transferring the Data—the drop Method
        2. A Multi-Functional Drop Target
          1. Text and Data Flavors
          2. FlavorMap and the SystemFlavorMap
          3. Establishing a Transfer Format when Several DataFlavors Are Available
          4. Transferring Text from the Drag Source to the Drop Target
        3. Providing Drag-Under Feedback
        4. Scrolling the Drop Target
      4. Implementing a Drag Source
        1. Implementing the JLabe1 Drag Source
        2. Creating a Transferable for a JLabel
      5. Using Drag-and-Drop with the JTree Component
        1. The FileTree Component
          1. Modifications to the FileTree Component
        2. The Drag Source Implementation
          1. The FileTreeDragSource Class
          2. Checking Whether the Drag Operation Is Valid
          3. Creating the Transferable
          4. Updating the FileTree on Completion of the Drag Operation
        3. The Drop Target
          1. The FileTreeDropTarget Class
            1. Checking for a Valid Drop Location
            2. Drag-Under Feedback
            3. Performing the Drop
          2. Performing the Movement of Files
            1. Removing Redundant File and Directory Paths
            2. Copying a Directory
            3. Copying a File
      6. Summary
      1. An Undo Example
        1. Text Component Events Revisited
        2. A Simple Way to Handle Undoable Edits
      2. Inside the Undo Package
        1. UndoableEdit and AbstractUndoableEdit
        2. A Simple UndoableEdit Example
      3. Compound Edits
        1. A Compound Edit Example
        2. Compound Edits and the Text Components
      4. The UndoManager Class
        1. Differences Between UndoManager and CompoundEdit
          1. The undo and redo Methods
          2. The die and addEdit Methods
          3. Significant and Insignificant Edits
          4. Other UndoManager Features
        2. Extending UndoManager
          1. Event Type
          2. Listener Registration
          3. Event Generation and Delivery
          4. Using the MonitorableUndoManager Class
      5. Summary