O'Reilly logo

Swing Hacks by Chris Adamson, Joshua Marinacci

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

Search Through JTables Easily #26
Chapter 3, Tables and Trees
|
133
HACK
The export button has a simple action listener that calls exportTable( ) on a
new
ExcelExporter. The trick to getting Excel to open the file with a double-
click on the desktop is to name the file with an .xls extension. It won’t be a
real Excel file, but the operating system will think that it is and pass it to
Excel anyway. Then Excel will look at the file, realize it’s actually a tab-
delimited text file, and load it with the right import filter.
Figure 3-10 shows what the program looks like.
H A C K
#26
Search Through JTables Easily Hack #26
Use this nifty TableModel decorator to search your JTables with minimal
fuss.
Tables have a tendency to get very big; thousands of rows are not uncom-
mon. But this causes some severe navigational issues for your users, like
extremely small scrollbar handles, which make it difficult for them to find
the information they need. One way to get around these navigational issues
is to allow your users to search the table data rather than displaying it all.
This hack shows you how to simply search your tables using the Apache
open source Lucene search engine.
JTable Search Strategy
Rather than a custom TableModel with integrated Lucene functionality, you
can build a
TableModel decorator instead. This will allow you to search pre-
existing
TableModels without modifying them directly.
This works by keeping a set of links to an internal table model based on
search criteria. For example, say you have 10 rows in your original table
model, and you have a search that limits the results to 5 of those rows. Your
inner
TableModel will remain unchanged, but your TableModel decorator will
have links to only five of the inner
TableModel rows—making it look like it
only has five rows of data.
Figure 3-10. An Excel-exporting JTable

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

Start Free Trial

No credit card required