public class PtFileChooser
extends java.awt.Container
If PtGUIUtilities.useFileDialog()
returns
true, then a java.awt.FileDialog is used. Otherwise a
javax.swing.JFileChooser is used. In general Mac OS X is the only
platform that where useFileDialog() will return true.
See PtGUIUtilities.useFileDialog()
for how
to set a runtime Java property to control whether FileDialog or
JFileChooser is used.
Note that this class should be wrapped in a try/finally block,
otherwise, under Windows, white boxes will appear in the common pane.
See JFileChooserBugFix
. Below is an example:
// Swap backgrounds and avoid white boxes in "common places" dialog JFileChooserBugFix jFileChooserBugFix = new JFileChooserBugFix(); Color background = null; PtFileChooser ptFileChooser; try { ptFileChooser = new PtFileChooser(_basicGraphFrame, title, JFileChooser.OPEN_DIALOG); ptFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); ptFileChooser.setCurrentDirectory(modelDirectory); ptFileChooser.addChoosableFileFilter(new DirectoryFilter()); int returnVal = ptFileChooser.showDialog(_basicGraphFrame, "Export HTML"); if (returnVal == JFileChooser.APPROVE_OPTION) { directory = ptFileChooser.getSelectedFile(); } } finally { try { if (ptFileChooser != null) { ptFileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); } } finally { jFileChooserBugFix.restoreBackground(background); } }
Only a subset of the methods in JFileChooser are declared. The method names follow the JFileChoose API because that API is more common.
Red (cxh) |
Red (cxh) |
Constructor and Description |
---|
PtFileChooser(java.awt.Frame parent,
java.lang.String title,
int mode)
Construct a PtFileChooser.
|
Modifier and Type | Method and Description |
---|---|
void |
addChoosableFileFilter(PtFilenameFilter filter)
Set the file name filter for the dialog.
|
java.io.File |
getCurrentDirectory()
Return the current directory.
|
java.io.File |
getSelectedFile()
Return the selected file as an absolute File (a File that is not relative).
|
void |
setCurrentDirectory(java.io.File directory)
Set the current directory.
|
void |
setFileSelectionMode(int mode)
Set the file selection mode.
|
void |
setSelectedFile(java.io.File file)
Set the selected file.
|
int |
showDialog(java.awt.Container parent,
java.lang.String approveButtonText)
Show the dialog.
|
add, add, add, add, add, addContainerListener, addImpl, addNotify, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getInsets, getLayout, getListeners, getMaximumSize, getMinimumSize, getMousePosition, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, isValidateRoot, layout, list, list, locate, minimumSize, paint, paintComponents, paramString, preferredSize, print, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, removeNotify, setComponentZOrder, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setFont, setLayout, transferFocusDownCycle, update, validate, validateTree
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, disable, disableEvents, dispatchEvent, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAccessibleContext, getBackground, getBaseline, getBaselineResizeBehavior, getBounds, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, revalidate, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeysEnabled, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
public PtFileChooser(java.awt.Frame parent, java.lang.String title, int mode)
If PtGUIUtilities.useFileDialog()
returns true, then a java.awt.FileDialog is used. Otherwise a
javax.swing.JFileChooser is used. The effect of the mode
argument varies depending on which type of dialog is used.
parent
- The parent component. Used with FileDialog,
ignored with JFileChooser.title
- The title of the dialogmode
- JFileChooser.OPEN_DIALOG, JFileChooser.SAVE_DIALOG,
or JFileChooser.CUSTOM_DIALOG. CUSTOM_DIALOG is ignored by FileDialog.
OPEN_DIALOG is a good default.public void addChoosableFileFilter(PtFilenameFilter filter)
filter
- The FilenameFilter to be used.public java.io.File getCurrentDirectory()
setCurrentDirectory(File)
public java.io.File getSelectedFile()
setSelectedFile(File)
public void setCurrentDirectory(java.io.File directory)
directory
- The current directory.getCurrentDirectory()
public void setFileSelectionMode(int mode)
If FileDialog is being used, then DIRECTORIES_ONLY sets the apple.awt.fileDialogForDirectories parameter. See https://developer.apple.com/library/mac/#documentation/Java/Reference/Java_PropertiesRef/Articles/JavaSystemProperties.html.
If this method is called with JFileChooser.DIRECTORIES_ONLY, then it should be called again with JFileChooser.FILES_AND_DIRECTORIES. Typically, the first call is in a try block and the second is in a finally block.
Note that if FileDialog is used, and mode is DIRECTORIES_ONLY, then this class must have been instantiated with a mode of FileBrowser.LOAD or JFileChooser.OPEN_DIALOG in the constructor.
mode
- One of JFileChooser.FILES_ONLY, JFileChooser.DIRECTORIES_ONLY or
JFileChooser.FILES_AND_DIRECTORIES.public void setSelectedFile(java.io.File file)
file
- The file to be selectedgetSelectedFile()
public int showDialog(java.awt.Container parent, java.lang.String approveButtonText)
parent
- Ignored with FileDialog, used with JFileChooser.approveButtonText
- The text for the approve button if JFileChooser is used.
If FileDialog is used, then this argument is ignored.