23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

13.2.5 Custom Dialogs<br />

<strong>Java</strong> I/O<br />

File choosers support three dialog types: open, save, and custom. The type is indicated by one<br />

of these three mnemonic constants:<br />

FileChooser.OPEN_DIALOG<br />

FileChooser.SAVE_DIALOG<br />

FileChooser.CUSTOM_DIALOG<br />

You set the type with the setDialogType() method or, less commonly, retrieve it with<br />

getDialogType():<br />

public int getDialogType()<br />

public void setDialogType(int dialogType)<br />

If you use a custom dialog, you should also set the dialog title, the text of the Approve<br />

button's label, the text of the Approve button's tooltip, and the Approve button mnemonic<br />

(shortcut key). Setting the Approve button's text automatically sets the dialog to custom type.<br />

There are five set and four get methods to handle these tasks:<br />

public void setDialogTitle(String dialogTitle)<br />

public String getDialogTitle()<br />

public void setApproveButtonToolTipText(String toolTipText)<br />

public String getApproveButtonToolTipText()<br />

public int getApproveButtonMnemonic()<br />

public void setApproveButtonMnemonic(int mnemonic)<br />

public void setApproveButtonMnemonic(char mnemonic)<br />

public void setApproveButtonText(String approveButtonText)<br />

public String getApproveButtonText()<br />

Use these methods sparingly. If you use them, you'll probably want to store the exact strings<br />

you use in a resource bundle so your code is easily localizable.<br />

When you're showing a custom dialog, you'll simply use the showDialog() method rather<br />

than showOpenDialog() or showSaveDialog() (since a custom dialog is neither):<br />

public int showDialog(Component parent, String approveButtonText)<br />

Suppose you want a file chooser that allows you to gzip files, then exit when the user presses<br />

the Cancel button. You can set the Approve button text to "GZIP," the Approve button tooltip<br />

to "Select a file, then press this button to gzip it," the Approve button mnemonic to the letter<br />

"g" (for gzip), and the dialog title to "Please choose a file to gzip:," as Example 13.5<br />

demonstrates. The chosen file is read from a file input stream. StreamCopier.copy() copies<br />

this file onto a file output stream chained to a gzip output stream that compresses the data.<br />

After both input and output streams are closed, the directory is rescanned so the compressed<br />

file will appear in the list.<br />

Example 13.5. GUIGZipper<br />

import java.io.*;<br />

import java.util.zip.*;<br />

import javax.swing.*;<br />

import com.macfaq.io.*;<br />

317

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

Saved successfully!

Ooh no, something went wrong!