28.12.2012 Views

Core JFC Java Foundation Classes 2nd edition - Read

Core JFC Java Foundation Classes 2nd edition - Read

Core JFC Java Foundation Classes 2nd edition - Read

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Customized Table Rendering and Editing 1421<br />

your read <strong>Core</strong> <strong>Java</strong> 2Volume 2:Advanced Features by Cay Horstmann<br />

and Gary Cornell, which covers this topic in detail.<br />

Of course, unless you happen to be designing or implementing both the<br />

source and target application, you probably don't have much choice as far as<br />

data representation is concerned, because the other application will only be<br />

able to handle a limited range of formats. As far as exchanging data with<br />

Microsoft Excel is concerned, the following factors have to be taken into<br />

account:<br />

1. Whichever way you are moving the data, it will have to go via<br />

the system clipboard. The AWT contains support for moving<br />

information between the system clipboard and a <strong>Java</strong> application.<br />

However, the implementation only allows you to transfer<br />

data encoded as strings. Although you can work around this<br />

restriction to encode more general data (such as images) as<br />

strings for the purposes of exporting them to the clipboard, this<br />

approach only works if the target application understands how<br />

to decode them from this form.<br />

2. Microsoft Excel supports a limited range of formats for data<br />

import from the clipboard. In particular, it will accept a value<br />

encoded as a string and install its content into a worksheet, provided<br />

that the row and column boundaries are properly delimited.<br />

Because of these restrictions, we are only going to be able to take string data<br />

from a JTable and copy it to Microsoft Excel and vice versa. However, this is<br />

not as restrictive as it sounds because quite a lot of non-string data, such as<br />

integers, floating point numbers, and dates, have a natural string representation<br />

that can be used for transfer and which is recognized by Microsoft Excel<br />

and, with a little extra work, you can add code to your table to convert the<br />

strings that you receive back into the original objects. Therefore, although<br />

the primary focus of the example that you'll see here is to establish the basic<br />

mechanism required to transfer strings, we'll build enough flexibility into it<br />

so that you can easily extend it (by subclassing) for more specific needs.<br />

The format used by Microsoft Excel to transfer data in string form is a simple<br />

one:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!