O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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. Front Cover
  2. Copyright
  3. Contents
  4. Credits
    1. About the Authors
    2. Contributors
    3. Acknowledgments
      1. Joshua
      2. Chris
  5. 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
  6. Chapter 1. Basic JComponents
    1. Create Image-Themed Components
    2. Don’t Settle for Boring Text Labels (1/2)
    3. Don’t Settle for Boring Text Labels (2/2)
    4. Fill Your Borders with Pretty Pictures
    5. Display Dates in a Custom Calendar
      1. Create the Images
      2. A Component to Paint
      3. Draw the Days of the Month
    6. Add a Watermark to a Text Component
    7. Watermark Your Scroll Panes
    8. Put a NASA Photo into the Background of a Text Area
    9. 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
    10. Blur Disabled Components
    11. Building a Drop-Down Menu Button
      1. Adding a Color Selection Panel
    12. Create Menus with Drop Shadows
    13. Add Translucence to Menus
      1. Make the Custom Menu Item
      2. Add a Custom JMenu
      3. Test It Out
      4. Future Ideas
  7. Chapter 2. Lists and Combos
    1. Filter JLists
    2. Add a Filter History
    3. Make JLists Checkable
    4. Make Different List Items Look Different (1/2)
    5. Make Different List Items Look Different (2/2)
    6. Reorder a JList with Drag-and-Drop (1/2)
    7. Reorder a JList with Drag-and-Drop (2/2)
    8. Animate Your JList Selections
    9. Turn Methods into List Renderers
      1. Building a Generic Renderer
      2. Putting It All Together
    10. Create a Collections-Aware JComboBox (1/2)
    11. Create a Collections-Aware JComboBox (2/2)
  8. Chapter 3. 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 (1/3)
    4. Let Your JTables Do the Sorting (2/3)
    5. Let Your JTables Do the Sorting (3/3)
    6. Create a JDBC Table Model
      1. Building Connectivity
      2. Testing Things Out
    7. Export Table Data to an Excel Spreadsheet
      1. Dealing with Formatting
    8. 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
    9. Animate JTree Drops
      1. The Code
      2. Running the Code
  9. Chapter 4. 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 (1/2)
    4. Real Windows Shortcut Support (2/2)
    5. Add Image Preview to File Choosers
    6. Preview ZIP and JAR Files
      1. Build File Proxies
      2. Build a Custom Filesystem View
      3. Put It All Together
  10. Chapter 5. 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
  11. Chapter 6. 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
  12. Chapter 7. 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
  13. Chapter 8. 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
      1. The Wonders of createFont()
      2. Testing Font Loading
    7. Build a Colorful Vector-Based Button
      1. Use Scaling to Your Advantage
      2. Hacking the Hack
    8. Add a Third Dimension to Swing
      1. The Problems with Java3D
      2. Faking Transparency
    9. Turn the Spotlight on Swing
      1. The Metaphor
      2. Add the Spotlight (1/2)
      3. Add the Spotlight (2/2)
  14. Chapter 9. Drag-and-Drop
    1. Drag-and-Drop with Files
    2. Handle Dropped URLs
      1. Drag Away
    3. Handle Dropped Images
      1. Grabbing the Drop
      2. Shut Up and Drag
    4. Handling Dropped Picts on Mac OS X
      1. Take a Breath and Run
    5. Translucent Drag-and-Drop
      1. A Rather Boring Cursor
      2. Translucence Rocks
      3. Drawing a Ghost
  15. Chapter 10. Audio
    1. Play a Sound in an Applet
      1. The Code
      2. No Browser, No Sound
    2. Play a Sound with JavaSound
      1. Putting JavaSound to Work
      2. Listen Up
    3. Play a Sound with Java Media Framework
      1. Installing JMF
      2. The Code
      3. Take JMF for a Spin
    4. Play a Sound with QuickTime for Java
      1. QuickTime Beating Up on JavaSound
      2. Compiling QuickTime Code
      3. Running the Code
    5. Add MP3 Support to JMF
      1. Add a Plug-In to JMF
      2. Simplicity Is Nice
      3. Distribute Your Program
    6. Build an Audio Waveform Display
      1. Some Basic Definitions
      2. Load the Raw Data
      3. Convert to Samples and Channels
      4. Creating a Single Waveform Display
      5. Create a Container
      6. Seeing Is Believing
    7. Play Non-Trivial Audio
      1. Grabbing a DataLine
      2. Big Files, Big Sound
    8. Show Audio Information While Playing Sound
      1. The Code
      2. Testing It Out
      3. Hacking the Hack
      4. Running the Hacked Hack
    9. Provide Audio Controls During Playback
      1. Theory to Practice
      2. Check It Out!
  16. Chapter 11. Native Integration and Packaging
    1. Launch External Programs on Windows
      1. The Power of Runtime.exec()
      2. Open a Text File
      3. Open a URL
      4. Open a Directory
    2. Open Files, Directories, and URLs on Mac OS X
      1. Using Open
      2. Handle Spaces
    3. Make Mac Applications Behave Normally
      1. Using the Apple System Properties
      2. Using the Mac’s Menu Bar
      3. Presenting an Appropriate Application Name
      4. Showing the Mac Grow Box
      5. Jump on the Brushed Metal Bandwagon
    4. Control iTunes on Mac OS X
      1. Apple Events
    5. Control iTunes Under Windows
      1. Working with COM
      2. Get Track Information
    6. Construct Single-Launch Applications
      1. Local Sockets
    7. Stuff Stuff in JARs
      1. An Obvious Secret
      2. Showing Off
      3. Packing Up
      4. Double-Clicking JARs
      5. And the Kitchen Sink
    8. Make Quick Look and Feel Changes
      1. Look and Feel Properties
      2. Text Components
      3. Use System Colors
    9. Create an Inverse Black-and-White Theme
      1. A Black-and-White Theme
  17. Chapter 12. Miscellany
    1. Display a Busy Cursor
      1. Pre-Generating Images
      2. Running the Animation
      3. Put It All Together
      4. And More...
    2. Fun with Keyboard Lights
    3. Create Demonstrations with the Robot Class
      1. I, Robot
    4. Check Your Mail with Swing
    5. Don’t Block the GUI
      1. To Block or Not to Block
    6. Code Models That Don’t Block
      1. Models Aren’t Always Dumb
      2. Running the Code
      3. Exposing the Threading
    7. Fire Events and Stay Bug Free
      1. The Problem
      2. And It Gets Worse
      3. Hacking a Solution
    8. Debug Your GUI
      1. Hijacking Output Streams
    9. Debug Components with a Custom Glass Pane
      1. Screens and Glass
    10. Mirror an Application
      1. Set Up a Window
      2. Become a Server or Client
      3. Send Mouse Events
      4. Receive Mouse Events
      5. Component Problems
    11. Add Velocity for Dynamic HTML
      1. Velocity and Templates
      2. Create the HTML
      3. Create a Data Object
      4. Create a Velocity Context
      5. Fill the Template with Values
      6. Sunny Outside?
    12. Get Large File Icons
    13. Make Frames Resize Dynamically
      1. A Word About Speed
  18. Index (1/4)
  19. Index (2/4)
  20. Index (3/4)
  21. Index (4/4)
  22. Other Java Resources from O'Reilly
  23. Back Cover