O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  


 
Buy the book!
Swing Hacks
By Joshua Marinacci, Chris Adamson
June 2005
More Info

How do these hacks stand up? Comment on a hack from the book by choosing the associated "Discuss" link below. You can also view the code from any of the hacks by clicking on the "Listing" or "Code" links. A number of hacks have been selected to be featured online in their entirety; you may view those hacks by clicking on the hack titles that are linked.

You can also download all the scripts and other files for this book here.

Jump to: Basic JComponents  | Lists and Combos  | Tables and Trees  | File Choosers  | Windows, Dialogs, and Frames  | Transparent and Animated Windows  | Text  | Rendering  | Drag-and-Drop  | Audio  | Native Integration and Packaging  | Miscellany

Basic JComponents

HACK
#1

Create Image-Themed Components
This hack shows how to use Swing's built-in image support to create a completely custom image-based user interface
[Discuss (0) | Link to this hack]

HACK
#2

Don't Settle for Boring Text Labels
JLabel is a Swing staple; but it's easy to spruce up boring labels with drop shadows, outlines, and even 3D text
[Discuss (0) | Link to this hack]

HACK
#3

Fill Your Borders with Pretty Pictures
Swing comes with a set of customizable borders, but sometimes you want more than they provide. This hack shows how to create a completely imagebased border that can be resized
[Discuss (0) | Link to this hack]

HACK
#4

Display Dates in a Custom Calendar
You can download calendar components from third parties, but real hackers can use Swing to build a custom calendar widget on their own
[Discuss (0) | Link to this hack]

HACK
#5

Add a Watermark to a Text Component
This hack will show how to create a custom image background for the , a complex Swing component that does not already support backgrounds or icons by default
[Discuss (0) | Link to this hack]

HACK
#6

Watermark Your Scroll Panes
This hack creates a text area with a tiled background image that is fixed, even when the text area scrolls, and also a fixed foreground image that appears above the text, much like the station badges now affixed to the lower-righthand corner of most TV broadcasts.
[Discuss (0) | Link to this hack]

HACK
#7

Put a NASA Photo into the Background of a Text Area
This hack will repurpose an existing web page, one of NASA's photo sites, by pulling their "Astronomy Picture of the Day" into the background of a text area.
[Discuss (0) | Link to this hack]

HACK
#8

Animate Transitions Between Tabs
This hack shows how to create animated transitions that play whenever the user switches tabs on a .
[Discuss (0) | Link to this hack]

HACK
#9

Blur Disabled Components
This hack explores creating how to perform a blur transformation on a Swing component.
[Discuss (1) | Link to this hack]

HACK
#10

Building a Drop-Down Menu Button
This hack shows how to build a color chooser as a proper drop-down component. It will behave like but without the extension headaches of Sun's version of the class.
[Discuss (0) | Link to this hack]

HACK
#11

Create Menus with Drop Shadows
This hack explores a simple way to create drop shadows on menus throughout an entire application with minimal code changes.
[Discuss (1) | Link to this hack]

HACK
#12

Add Translucence to Menus
In this hack I will show you how to add true translucency to your menus with only a slight modification to your program.
[Discuss (0) | Link to this hack]

Lists and Combos

HACK
#13

Filter JLists
Make your 1,000-item list a lot more manageable
[Discuss (0) | Link to this hack]

HACK
#14

Add a Filter History
Remember previous searches and research with one click
[Discuss (0) | Link to this hack]

HACK
#15

Make JLists Checkable
Avoid losing 50 selections to an unshifted click
[Discuss (1) | Link to this hack]

HACK
#16

Make Different List Items Look Different
An in-progress download shouldn't look like a completed one
[Discuss (1) | Link to this hack]

HACK
#17

Reorder a JList with Drag-and-Drop
Let users put things where they want
[Discuss (0) | Link to this hack]

HACK
#18

Animate Your JList Selections
Fading in and catching the eye
[Discuss (0) | Link to this hack]

HACK
#19

Turn Methods into List Renderers
By using a little bit of reflection, you can make a generic that can render data using any method at runtime
[Discuss (0) | Link to this hack]

HACK
#20

Create a Collections-Aware JComboBox
You've moved on from Vector; your combo boxes should, too
[Discuss (0) | Link to this hack]

Tables and Trees

HACK
#21

Size Your Columns to Suit Your JTable's Contents
A one-digit column does not need 100 pixels of dead space. You know this; your JTables should, too
[Discuss (0) | Link to this hack]

HACK
#22

Add Column Selection to JTables
So, why can't I select a column by clicking on its header?
[Discuss (0) | Link to this hack]

HACK
#23

Let Your JTables Do the Sorting
Why doesn't Swing already offer this? Oh well, here's how to do it yourself
[Discuss (0) | Link to this hack]

HACK
#24

Create a JDBC Table Model
Bring your database tables into Swing with a minimum of hassle
[Discuss (0) | Link to this hack]

HACK
#25

Export Table Data to an Excel Spreadsheet
I don't want an entire spreadsheet API, I just want to get a table of values into Excel
[Discuss (0) | Link to this hack]

HACK
#26

Search Through JTables Easily
Use this nifty TableModel decorator to search your JTables with minimal fuss
[Discuss (0) | Link to this hack]

HACK
#27

Animate JTree Drops
Who said working with tree paths was hard? Now you can reorganize tree hierarchies with drag-and-drop
The Code
[Discuss (0) | Link to this hack]

File Choosers

HACK
#28

Add a Right-Click Context Menu to the JFileChooser
Improve the native platform fidelity of the by adding a contextual menu that lets the user create new folders and delete files
[Discuss (0) | Link to this hack]

HACK
#29

Display Shortcuts in the JFileChooser
This hack will customize the to recognize shortcut (linked) folders and overlay them with a link graphic, mimicking the native Windows File Explorer
[Discuss (0) | Link to this hack]

HACK
#30

Real Windows Shortcut Support
Support Windows shortcuts by actually opening and parsing files with the under-documented LNK format
[Discuss (0) | Link to this hack]

HACK
#31

Add Image Preview to File Choosers
This hack will show you how to add an image previewer to a , and it will set you on the way toward building your own customizations
[Discuss (0) | Link to this hack]

HACK
#32

Preview ZIP and JAR Files
This hack will show you how to customize the file chooser to let users navigate and load files from inside a ZIP or JAR file archive
[Discuss (0) | Link to this hack]

Windows, Dialogs, and Frames

HACK
#33

Window Snapping
Make your windows snap to the edges of the screen by using a special event listener
[Discuss (0) | Link to this hack]

HACK
#34

Make a Draggable Window
Drag a window by clicking on its background using a special event listener
[Discuss (0) | Link to this hack]

HACK
#35

Add Windows Resize Icons
The Windows resize icons aren't built into Java. Here's how to make your own
[Discuss (0) | Link to this hack]

HACK
#36

Add Status Bars to Windows
Lots of applications use a bottom-of-window panel to communicate status. But Swing doesn't provide a consistent way to do this. So, you need to provide it yourself
[Discuss (0) | Link to this hack]

HACK
#37

Save Window Settings
Make sure your windows always show up right where you left them, even after a program restarts, by saving the window position and size automatically
[Discuss (0) | Link to this hack]

HACK
#38

Earthquake Dialog
Make sure your users really know they got their password wrong
[Discuss (0) | Link to this hack]

HACK
#39

Spin Open a Detail Pane
You don't want to bombard the user with details, but you don't want to hide them either. Here's a way to let the user pop open a More Info widget
[Discuss (0) | Link to this hack]

HACK
#40

Minimize to a Mini-Frame
When you want your program to have a smaller window but still be on the screen, try building a mini-mod
[Discuss (0) | Link to this hack]

Transparent and Animated Windows

HACK
#41

Transparent Windows
Create translucent and shaped windows, while avoiding native code, with clever use of a screenshot
[Discuss (0) | Link to this hack]

HACK
#42

Make Your Frame Dissolve
Create animated frame dissolves using two screenshots and some clever graphics code
[Discuss (0) | Link to this hack]

HACK
#43

Create Custom Tool Tips
Replace the standard rollover tool tip with an attractive custom version, including a border and rounded corners
[Discuss (1) | Link to this hack]

HACK
#44

Turn Dialogs into Frame-Anchored Sheets
One of Mac OS X's best ideas is binding the dialog to the window it blocks. This hack shows you how to mimic this in Swing
[Discuss (0) | Link to this hack]

HACK
#45

Animating a Sheet Dialog
By animating the sheet's appearance and disappearance, you give the user a better clue that his attention is required. Plus, it looks cool
[Discuss (0) | Link to this hack]

HACK
#46

Slide Notes Out from the Taskbar
Pop up a note above the taskbar when your application wants attention
[Discuss (0) | Link to this hack]

HACK
#47

Indefinite Progress Indicator
Despite its numerous advanced widgets, Swing offers no efficient way to show that a task of unknown length is in progress. This hack presents two solutions to address this issue
[Discuss (0) | Link to this hack]

Text

HACK
#48

Make Text Components Searchable
This hack will show you how to add incremental search to a text area as a simple document listener, making it very easy to integrate with your existing software
[Discuss (0) | Link to this hack]

HACK
#49

Force Text Input into Specific Formats
Use Java's powerful pattern matching to enforce rules on typed input
[Discuss (0) | Link to this hack]

HACK
#50

Auto-Completing Text Fields
Typing in a whole URL is a pain. When the user starts to type, complete his text with previously entered options, and let the user select one instead of typing the whole URL
[Discuss (0) | Link to this hack]

HACK
#51

Write Backward Text
Baffle your friends by turning their text into its mirror image
[Discuss (0) | Link to this hack]

HACK
#52

Use HTML and CSS in Text Components
Spruce up your plain JLabels and buttons using HTML and CSS effects, such as underlines, color, and even embedded tables
[Discuss (0) | Link to this hack]

HACK
#53

Use Global Anti-Aliased Fonts
Think Swing apps always look ugly because of the chunky fonts? Finally, you can do something about it
[Discuss (0) | Link to this hack]

HACK
#54

Anti-Aliased Text Without Code
Draw anti-aliased text without any code changes at all using two clever tricks introduced in Java 5.0
[Discuss (0) | Link to this hack]

HACK
#55

Anti-Aliased Text with a Custom Look and Feel
Another way to get smooth text is to use a custom Look and Feel to avoid the fragile Java 5.0 APIs
[Discuss (0) | Link to this hack]

Rendering

HACK
#56

Create a Magnifying Glass Component
Zoom in on those pixels with a little creative abuse of the AWT's debuggingoriented class
[Discuss (0) | Link to this hack]

HACK
#57

Create a Global Right-Click
Give your application a right-click context menu without having to add a listener to every component
[Discuss (0) | Link to this hack]

HACK
#58

Block a Window Without a Modal Dialog
Block the input in a single window during long operations without stopping your entire application
[Discuss (0) | Link to this hack]

HACK
#59

Create a Color Eyedropper
Enhance your color pickers with an eyedropper tool that grabs a color from anywhere on the screen
[Discuss (0) | Link to this hack]

HACK
#60

Changing Fonts Throughout Your Application
Get a quick font face-lift, without having to write a whole Look and Feel
[Discuss (0) | Link to this hack]

HACK
#61

Load New Fonts at Runtime
Who cares what fonts your users have? Bundle the fonts you want your application to use and load those fonts dynamically
[Discuss (0) | Link to this hack]

HACK
#62

Build a Colorful Vector-Based Button
Build a resolution-independent OS X-style button using scalable graphics code
[Discuss (0) | Link to this hack]

HACK
#63

Add a Third Dimension to Swing
User interfaces have stuck to 2D drawing for many years. Today, Swing and Java3D give you a chance to go one step further and add 3D widgets to your UI
[Discuss (0) | Link to this hack]

HACK
#64

Turn the Spotlight on Swing
Users often get lost when using applications, as if they were in total darkness. Why don't you turn on a spotlight to show them the way
[Discuss (0) | Link to this hack]

Drag-and-Drop

HACK
#65

Drag-and-Drop with Files
Drag files from your application directly to the desktop, complete with translucent icons
[Discuss (0) | Link to this hack]

HACK
#66

Handle Dropped URLs
Drag-and-drop is like a box of chocolates; you never know what you're going to get…
[Discuss (0) | Link to this hack]

HACK
#67

Handle Dropped Images
I spy, with my little method, something that doesn't support …arrrgh
[Discuss (0) | Link to this hack]

HACK
#68

Handling Dropped Picts on Mac OS X
For Mac applications that provide only the legacy Pict flavor of drops, QuickTime for Java offers a Mac-specific solution
[Discuss (0) | Link to this hack]

HACK
#69

Translucent Drag-and-Drop
The Java implementation of drag-and-drop offers poor visual feedback. This hack shows how to provide more information and a better-looking response to the user
[Discuss (0) | Link to this hack]

Audio

HACK
#70

Play a Sound in an Applet
If you're forced to write to the old Applet API for sound, here's how you do it. Good luck
The Code
[Discuss (0) | Link to this hack]

HACK
#71

Play a Sound with JavaSound
Get a small clip to play from memory with a lot less hassle
[Discuss (0) | Link to this hack]

HACK
#72

Play a Sound with Java Media Framework
Use the Java Media Framework for better performance and support for more audio formats
The Code
[Discuss (0) | Link to this hack]

HACK
#73

Play a Sound with QuickTime for Java
Using QuickTime, you can play even more kinds of sounds, but only on two operating systems
[Discuss (0) | Link to this hack]

HACK
#74

Add MP3 Support to JMF
MP3s are everywhere, and by installing a plug-in you can use them with Java Media Framework, too
[Discuss (0) | Link to this hack]

HACK
#75

Build an Audio Waveform Display
With a little understanding of audio data formats, you can easily build a basic graphical audio display
[Discuss (0) | Link to this hack]

HACK
#76

Play Non-Trivial Audio
When loading an entire audio clip into memory is a bad idea (or just impossible), you have to take JavaSound responsibilities into your own hands
[Discuss (0) | Link to this hack]

HACK
#77

Show Audio Information While Playing SoundHack
Providing visual feedback for JavaSound audio, or at least trying to…
The Code
[Discuss (0) | Link to this hack]

HACK
#78

Provide Audio Controls During Playback
Let your users take control of JavaSound playback
[Discuss (0) | Link to this hack]

Native Integration and Packaging

HACK
#79

Launch External Programs on Windows
With one simple command you can tell Windows to open files, directories, and URLs on your behalf
[Discuss (0) | Link to this hack]

HACK
#80

Open Files, Directories, and URLs on Mac OS X
Open files, directories, and URLs in external programs right from your Swing app
[Discuss (0) | Link to this hack]

HACK
#81

Make Mac Applications Behave Normally
Setting a few system properties will make your application seem more like other Mac apps
[Discuss (0) | Link to this hack]

HACK
#82

Control iTunes on Mac OS X
With a few bits of AppleScript, you can monitor and control Macintosh iTunes from your own application
[Discuss (0) | Link to this hack]

HACK
#83

Control iTunes Under Windows
Use a simple open source library to monitor and control Windows iTunes from your Swing application
[Discuss (0) | Link to this hack]

HACK
#84

Construct Single-Launch Applications
Only allow one instance of a program, notifying the existing instance when the user tries to launch a new one
[Discuss (0) | Link to this hack]

HACK
#85

Stuff Stuff in JARs
Hide images, sounds, and more inside JAR files
[Discuss (0) | Link to this hack]

HACK
#86

Make Quick Look and Feel Changes
Customize Metal with custom fonts, colors, and even system-bound properties using just a few API calls
[Discuss (0) | Link to this hack]

HACK
#87

Create an Inverse Black-and-White Theme
Create a custom black-and-white theme for monochrome LCD displays using a few simple UIManager calls
[Discuss (0) | Link to this hack]

Miscellany

HACK
#88

Display a Busy Cursor
Use the () method and an animation thread to show a frame's busy status
[Discuss (0) | Link to this hack]

HACK
#89

Fun with Keyboard Lights
Flash the Caps Lock, Num Lock, and Scroll Lock keys for extra user feedback
[Discuss (0) | Link to this hack]

HACK
#90

Create Demonstrations with the Robot Class
Use the Robot class to control the mouse cursor and create interactive software features
[Discuss (0) | Link to this hack]

HACK
#91

Check Your Mail with Swing
Add email checking to your application with just a few method calls
[Discuss (0) | Link to this hack]

HACK
#92

Don't Block the GUI
Thread your heavy lifting so the event-dispatch thread stays responsive
[Discuss (0) | Link to this hack]

HACK
#93

Code Models That Don't Block
Models should know that they're doing work on another thread
[Discuss (0) | Link to this hack]

HACK
#94

Fire Events and Stay Bug Free
Most developers think that writing an event-firing method is trivial. Most developers are wrong
[Discuss (0) | Link to this hack]

HACK
#95

Debug Your GUI
Standard out and err aren't just for log files anymore
[Discuss (0) | Link to this hack]

HACK
#96

Debug Components with a Custom Glass Pane
Show component boundaries at runtime using a glass pane
[Discuss (0) | Link to this hack]

HACK
#97

Mirror an Application
With creative use of the AWT event log, you can bind two instances of an application together over a socket, creating a mirroring effect
[Discuss (0) | Link to this hack]

HACK
#98

Add Velocity for Dynamic HTML
Use the Velocity template engine to mimic server-side web technologies in your Swing application
[Discuss (0) | Link to this hack]

HACK
#99

Get Large File Icons
Using an undocumented Windows-only class, you can retrieve large, full-color file icons from the operating system
[Discuss (0) | Link to this hack]

HACK
#100

Make Frames Resize Dynamically
Make your application feel more responsive by turning on dynamic layout
[Discuss (0) | Link to this hack]


O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.