Cover image for Swing Hacks

Book description

Swing Hacks helps Java developers move beyond the basics of Swing, the graphical user interface (GUI) standard since Java 2. If you're a Java developer looking to build enterprise applications with a first-class look and feel, Swing is definitely one skill you need to master. This latest title from O'Reilly is a reference to the cool stuff in Swing. It's about the interesting things you learn over the years--creative, original, even weird hacks--the things that make you say, "I didn't know you could even do that with Swing!" Swing Hacks will show you how to extend Swing's rich component set in advanced and sometimes non-obvious ways. The book touches upon the entire Swing gamut-tables, trees, sliders, spinners, progress bars, internal frames, and text components. Detail is also provided on JTable/JTree, threaded component models, and translucent windows. You'll learn how to filter lists, power-up trees and tables, and add drag-and-drop support. Swing Hacks will show you how to do fun things that will directly enhance your own applications. Some are visual enhancements to make your software look better. Some are functional improvements to make your software do something it couldn't do before. Some are even just plain silly, in print only to prove it could be done. The book will also give you give you a small glimpse of the applications coming in the future. New technology is streaming into the Java community at a blistering rate, and it gives application developers a whole new set of blocks to play with. With its profusion of tips and tricks, Swing Hacks isn't just for the developer who wants to build a better user interface. It's also ideally suited for client-side Java developers who want to deliver polished applications, enthusiasts who want to push Java client application boundaries, and coders who want to bring powerful techniques to their own applications. Whatever your programming needs, Swing Hacks is packed with programming lessons that increase your competency with interface-building tools.

Table of Contents

  1. Contents
  2. Credits
    1. About the Authors
    2. Contributors
    3. Acknowledgments
      1. Joshua
      2. Chris
  3. Preface
    1. Why Swing Hacks?
    2. How to Use This Book
    3. How This Book Is Organized
    4. Conventions Used in This Book
    5. Using Code Examples
    6. How to Contact Us
    7. Got a Hack?
    8. Safari Enabled
  4. Basic JComponents
    1. Create Image-Themed Components
    2. Don’t Settle for Boring Text Labels
    3. Fill Your Borders with Pretty Pictures
    4. Display Dates in a Custom Calendar
      1. Create the Images
      2. A Component to Paint
      3. Draw the Days of the Month
    5. Add a Watermark to a Text Component
    6. Watermark Your Scroll Panes
    7. Put a NASA Photo into the Background of a Text Area
    8. Animate Transitions Between Tabs
      1. Building a Basic Tabbed Pane
      2. Scheduling the Animation
      3. Drawing the Animation
      4. Putting It All Together
      5. Another Example
    9. Blur Disabled Components
    10. Building a Drop-Down Menu Button
      1. Adding a Color Selection Panel
    11. Create Menus with Drop Shadows
    12. Add Translucence to Menus
      1. Make the Custom Menu Item
      2. Add a Custom JMenu
      3. Test It Out
      4. Future Ideas
  5. Lists and Combos
    1. Filter JLists
    2. Add a Filter History
    3. Make JLists Checkable
    4. Make Different List Items Look Different
    5. Reorder a JList with Drag-and-Drop
    6. Animate Your JList Selections
    7. Turn Methods into List Renderers
      1. Building a Generic Renderer
      2. Putting It All Together
    8. Create a Collections-Aware JComboBox
  6. Tables and Trees
    1. Size Your Columns to Suit Your JTable’s Contents
      1. Resetting Column Widths
      2. Accounting for Header Cells
      3. Hacking the Hack
    2. Add Column Selection to JTables
    3. Let Your JTables Do the Sorting
    4. Create a JDBC Table Model
      1. Building Connectivity
      2. Testing Things Out
    5. Export Table Data to an Excel Spreadsheet
      1. Dealing with Formatting
    6. Search Through JTables Easily
      1. JTable Search Strategy
      2. Decorating the TableModel
      3. Creating Logical Links to the Inner Table Model
      4. Indexing
      5. Searching
        1. Getting results from the index
        2. Recreating the inner table model links
      6. Try It Out
      7. Finishing Touches
        1. Listen to inner table updates
        2. Clear search results for blank search
      8. Wrapping Up
    7. Animate JTree Drops
      1. The Code
      2. Running the Code
  7. File Choosers
    1. Add a Right-Click Context Menu to the JFileChooser
      1. The Problem
    2. Display Shortcuts in the JFileChooser
    3. Real Windows Shortcut Support
    4. Add Image Preview to File Choosers
    5. Preview ZIP and JAR Files
      1. Build File Proxies
      2. Build a Custom Filesystem View
      3. Put It All Together
  8. Windows, Dialogs, and Frames
    1. Window Snapping
    2. Make a Draggable Window
    3. Add Windows Resize Icons
      1. A Tale of Two Icons
      2. The Icon Interface
      3. The Explorer Icon
      4. The Office Icon
    4. Add Status Bars to Windows
      1. Standard Status Bars
      2. Painting Panel Details
      3. Add the Corner Icon
      4. Add the Left Component
      5. Add a Separator Panel
      6. And the Rest...
      7. Running the Hack
    5. Save Window Settings
      1. The Window Saver Class
    6. Earthquake Dialog
      1. Exterior Animation
      2. Shake, Rattle, and Roll
    7. Spin Open a Detail Pane
      1. The Invisible Man
      2. Now You See Me
    8. Minimize to a Mini-Frame
      1. I Shall Call Him...Mini-Me
      2. Minimize the Frame
      3. Restore the Frame
  9. Transparent and Animated Windows
    1. Transparent Windows
    2. Make Your Frame Dissolve
      1. Prepare the Dissolve
      2. Run the Animation
      3. Do the Drawing
      4. Create a Genie Effect
    3. Create Custom Tool Tips
      1. Install the Tool Tip
    4. Turn Dialogs into Frame-Anchored Sheets
      1. Why Sheets Rock
      2. Use the Glass Pane
    5. Animating a Sheet Dialog
      1. Animate the Sheet
      2. Self-Painting
    6. Slide Notes Out from the Taskbar
      1. Figure Out Where You Are
      2. Running the Hack
      3. Hacking the Hack
    7. Indefinite Progress Indicator
      1. The Swing Solution
      2. Picture as Indicator
      3. The Glass Pane as an Indicator
      4. Build the Circular Shape
      5. Paint the Indicator
      6. Run the Animation Thread
  10. Text
    1. Make Text Components Searchable
      1. A Basic Search Class
      2. Running the Search
      3. Adding Search to Swing Components
      4. Hacking the Hack
    2. Force Text Input into Specific Formats
      1. Constraining a Document
      2. Adding Constrained Text Fields
    3. Auto-Completing Text Fields
      1. A Self-Completing Text Field
      2. Test Out Auto-Complete
    4. Write Backward Text
      1. Messing with JLabel
      2. Mirror, Mirror on the Wall
    5. Use HTML and CSS in Text Components
      1. Here’s the Trick
    6. Use Global Anti-Aliased Fonts
      1. The Problem
    7. Anti-Aliased Text Without Code
      1. The Java 5.0 Trick
    8. Anti-Aliased Text with a Custom Look and Feel
      1. The Wrap Look and Feel
  11. Rendering
    1. Create a Magnifying Glass Component
      1. Build the Magnifying Glass
      2. Testing the Magnifier Out
      3. Hacking the Hack
    2. Create a Global Right-Click
    3. Block a Window Without a Modal Dialog
      1. Blocking Basics
      2. Build a Test Process
      3. Putting It All Together
    4. Create a Color Eyedropper
    5. Changing Fonts Throughout Your Application
      1. Changing the Default Fonts
      2. Fonts ’R’ Us
    6. Load New Fonts at Runtime