📄 jfilechooser.java
字号:
* * @see #setSelectedFile(File) */ public File getSelectedFile() { return selectedFile; } /** * Sets the selected file and sends a {@link PropertyChangeEvent} to all * registered listeners. The property name is * {@link #SELECTED_FILE_CHANGED_PROPERTY}. * * @param file the file (<code>null</code> permitted). */ public void setSelectedFile(File file) { if (selectedFile != file) { File old = selectedFile; selectedFile = file; firePropertyChange(SELECTED_FILE_CHANGED_PROPERTY, old, selectedFile); } } /** * Returns the selected file or files. * * @return An array of the selected files, or <code>null</code> if there are * no selected files. */ public File[] getSelectedFiles() { if (selectedFiles != null) return selectedFiles; if (selectedFile != null) return new File[] { selectedFile }; return null; } /** * Sets the selected files and sends a {@link PropertyChangeEvent} (with the * name {@link #SELECTED_FILES_CHANGED_PROPERTY}) to all registered * listeners. * * @param selectedFiles the selected files (<code>null</code> permitted). */ public void setSelectedFiles(File[] selectedFiles) { if (this.selectedFiles != selectedFiles) { File[] old = this.selectedFiles; this.selectedFiles = selectedFiles; firePropertyChange(SELECTED_FILES_CHANGED_PROPERTY, old, selectedFiles); } if (selectedFiles != null) setSelectedFile(selectedFiles[0]); } /** * Returns the current directory. * * @return The current directory. */ public File getCurrentDirectory() { return currentDir; } /** * Sets the current directory and fires a {@link PropertyChangeEvent} (with * the property name {@link #DIRECTORY_CHANGED_PROPERTY}) to all registered * listeners. If <code>dir</code> is <code>null</code>, the current * directory is set to the default directory returned by the file system * view. * * @param dir the new directory (<code>null</code> permitted). * * @see FileSystemView#getDefaultDirectory() */ public void setCurrentDirectory(File dir) { if (currentDir != dir || dir == null) { if (dir == null) dir = fsv.getDefaultDirectory(); File old = currentDir; currentDir = dir; firePropertyChange(DIRECTORY_CHANGED_PROPERTY, old, currentDir); } } /** * Called by the UI delegate when the parent directory is changed. */ public void changeToParentDirectory() { if (fsv.getParentDirectory(currentDir) != null) setCurrentDirectory(fsv.getParentDirectory(currentDir)); } /** * Rescans the current directory (this is handled by the UI delegate). */ public void rescanCurrentDirectory() { getUI().rescanCurrentDirectory(this); } /** * Ensures the the specified file is visible (this is handled by the * UI delegate). * * @param f the file. */ public void ensureFileIsVisible(File f) { getUI().ensureFileIsVisible(this, f); } /** * Displays the file chooser in a modal dialog using the * {@link #OPEN_DIALOG} type. * * @param parent the parent component. * * @return A return value indicating how the dialog was closed (one of * {@link #APPROVE_OPTION}, {@link #CANCEL_OPTION} and * {@link #ERROR_OPTION}). * * @throws HeadlessException DOCUMENT ME! */ public int showOpenDialog(Component parent) throws HeadlessException { JDialog d = createDialog(parent); // FIXME: Remove when we get ancestor property d.setTitle("Open"); setDialogType(OPEN_DIALOG); retval = ERROR_OPTION; d.pack(); d.show(); return retval; } /** * Displays the file chooser in a modal dialog using the * {@link #SAVE_DIALOG} type. * * @param parent the parent component. * * @return A return value indicating how the dialog was closed (one of * {@link #APPROVE_OPTION}, {@link #CANCEL_OPTION} and * {@link #ERROR_OPTION}). * * @throws HeadlessException DOCUMENT ME! */ public int showSaveDialog(Component parent) throws HeadlessException { JDialog d = createDialog(parent); setDialogType(SAVE_DIALOG); retval = ERROR_OPTION; d.pack(); d.show(); return retval; } /** * Displays the file chooser in a modal dialog using the * {@link #CUSTOM_DIALOG} type. * * @param parent the parent component. * * @return A return value indicating how the dialog was closed (one of * {@link #APPROVE_OPTION}, {@link #CANCEL_OPTION} and * {@link #ERROR_OPTION}). * * @throws HeadlessException DOCUMENT ME! */ public int showDialog(Component parent, String approveButtonText) throws HeadlessException { JDialog d = createDialog(parent); setApproveButtonText(approveButtonText); setDialogType(CUSTOM_DIALOG); retval = ERROR_OPTION; d.pack(); d.show(); return retval; } /** * Creates a modal dialog in which to display the file chooser. * * @param parent the parent component. * * @return The dialog. * * @throws HeadlessException DOCUMENT ME! */ protected JDialog createDialog(Component parent) throws HeadlessException { Frame toUse = (Frame) SwingUtilities.getAncestorOfClass(Frame.class, parent); if (toUse == null) toUse = SwingUtilities.getOwnerFrame(); JDialog dialog = new JDialog(toUse); setSelectedFile(null); dialog.getContentPane().add(this); dialog.setModal(true); dialog.invalidate(); dialog.repaint(); return dialog; } /** * Returns the flag that controls whether or not the control buttons are * shown on the file chooser. * * @return A boolean. * * @see #setControlButtonsAreShown(boolean) */ public boolean getControlButtonsAreShown() { return controlButtonsShown; } /** * Sets the flag that controls whether or not the control buttons are * shown and, if it changes, sends a {@link PropertyChangeEvent} (with the * property name {@link #CONTROL_BUTTONS_ARE_SHOWN_CHANGED_PROPERTY}) to * all registered listeners. * * @param b the new value for the flag. */ public void setControlButtonsAreShown(boolean b) { if (controlButtonsShown != b) { controlButtonsShown = b; firePropertyChange(CONTROL_BUTTONS_ARE_SHOWN_CHANGED_PROPERTY, ! controlButtonsShown, controlButtonsShown); } } /** * Returns the type of file chooser. * * @return {@link #OPEN_DIALOG}, {@link #SAVE_DIALOG} or * {@link #CUSTOM_DIALOG}. * * @see #setDialogType(int) */ public int getDialogType() { return dialogType; } /** * Sets the dialog type and fires a {@link PropertyChangeEvent} (with the * property name {@link #DIALOG_TYPE_CHANGED_PROPERTY}) to all * registered listeners. * * @param dialogType the dialog type (one of: {@link #OPEN_DIALOG}, * {@link #SAVE_DIALOG}, {@link #CUSTOM_DIALOG}). * * @throws IllegalArgumentException if <code>dialogType</code> is not valid. */ public void setDialogType(int dialogType) { if (dialogType != OPEN_DIALOG && dialogType != SAVE_DIALOG && dialogType != CUSTOM_DIALOG) throw new IllegalArgumentException("Choose allowable dialogType."); if (this.dialogType != dialogType) { int old = this.dialogType; this.dialogType = dialogType; firePropertyChange(DIALOG_TYPE_CHANGED_PROPERTY, old, this.dialogType); } } /** * Sets the dialog title and sends a {@link PropertyChangeEvent} (with the * property name {@link #DIALOG_TITLE_CHANGED_PROPERTY}) to all * registered listeners. * * @param dialogTitle the dialog title (<code>null</code> permitted). * * @see #getDialogTitle() */ public void setDialogTitle(String dialogTitle) { if (this.dialogTitle != dialogTitle) { String old = this.dialogTitle; this.dialogTitle = dialogTitle; firePropertyChange(DIALOG_TITLE_CHANGED_PROPERTY, old, this.dialogTitle); } } /** * Returns the dialog title. * * @return The dialog title (possibly <code>null</code>). * * @see #setDialogTitle(String) */ public String getDialogTitle() { return dialogTitle; } /** * Sets the tool tip text for the approve button and sends a * {@link PropertyChangeEvent} (with the property name * {@link #APPROVE_BUTTON_TOOL_TIP_TEXT_CHANGED_PROPERTY}) to all * registered listeners. * * @param toolTipText the text. */ public void setApproveButtonToolTipText(String toolTipText) { if (approveButtonToolTipText != toolTipText) { String oldText = approveButtonToolTipText; approveButtonToolTipText = toolTipText; firePropertyChange(APPROVE_BUTTON_TOOL_TIP_TEXT_CHANGED_PROPERTY, oldText, approveButtonToolTipText); } } /** * Returns the tool tip text for the approve button. * * @return The tool tip text for the approve button. * * @see #setApproveButtonToolTipText(String) */ public String getApproveButtonToolTipText() { return approveButtonToolTipText; } /** * Returns the approve button mnemonic, or zero if no mnemonic has been set. * * @return The approve button mnemonic. * * @see #setApproveButtonMnemonic(int) */ public int getApproveButtonMnemonic() { return approveButtonMnemonic; } /** * Sets the mnemonic for the approve button and sends a * {@link PropertyChangeEvent} (with the property name * {@link #APPROVE_BUTTON_MNEMONIC_CHANGED_PROPERTY}) to all registered * listeners. * * @param mnemonic the mnemonic. * * @see #setApproveButtonMnemonic(char) */ public void setApproveButtonMnemonic(int mnemonic) { if (approveButtonMnemonic != mnemonic) { int oldMnemonic = approveButtonMnemonic; approveButtonMnemonic = mnemonic; firePropertyChange(APPROVE_BUTTON_MNEMONIC_CHANGED_PROPERTY, oldMnemonic, approveButtonMnemonic); } } /** * Sets the mnemonic for the approve button and sends a * {@link PropertyChangeEvent} (with the property name * {@link #APPROVE_BUTTON_MNEMONIC_CHANGED_PROPERTY}) to all registered * listeners. * * @param mnemonic the mnemonic. * * @see #setApproveButtonMnemonic(int) */ public void setApproveButtonMnemonic(char mnemonic) { setApproveButtonMnemonic((int) Character.toUpperCase(mnemonic)); } /** * Sets the approve button text and fires a {@link PropertyChangeEvent} * (with the property name {@link #APPROVE_BUTTON_TEXT_CHANGED_PROPERTY}) to * all registered listeners. * * @param approveButtonText the text (<code>null</code> permitted). * * @see #getApproveButtonText() */ public void setApproveButtonText(String approveButtonText) { if (this.approveButtonText != approveButtonText) { String oldText = this.approveButtonText; this.approveButtonText = approveButtonText; firePropertyChange(APPROVE_BUTTON_TEXT_CHANGED_PROPERTY, oldText, this.approveButtonText); } } /** * Returns the approve button text. * * @return The approve button text (possibly <code>null</code>). * * @see #setApproveButtonText(String) */ public String getApproveButtonText() { return approveButtonText; } /** * Returns the available file filters for this file chooser. * * @return The available file filters. */ public FileFilter[] getChoosableFileFilters() { return (FileFilter[]) choosableFilters.toArray(new FileFilter[choosableFilters.size()]); } /** * Adds a file filter to the list of available filters and sends a * {@link PropertyChangeEvent} (with the property name * {@link #CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY}) to all registered * listeners. * * @param filter the filter. */ public void addChoosableFileFilter(FileFilter filter) { FileFilter[] old = getChoosableFileFilters(); choosableFilters.add(filter); FileFilter[] newFilters = getChoosableFileFilters(); firePropertyChange(CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY, old, newFilters); } /** * Removes a file filter from the list of available filters and sends a * {@link PropertyChangeEvent} (with the property name * {@link #CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY}) to all registered * listeners. * * @param f the file filter. * * @return <code>true</code> if the filter was removed and * <code>false</code> otherwise. */ public boolean removeChoosableFileFilter(FileFilter f) { FileFilter[] old = getChoosableFileFilters(); if (! choosableFilters.remove(f)) return false; FileFilter[] newFilters = getChoosableFileFilters(); firePropertyChange(CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY, old, newFilters); return true; } /** * Clears the list of choosable file filters and installs the 'accept all' * filter from the UI delegate. */ public void resetChoosableFileFilters() { choosableFilters.clear(); choosableFilters.add(getUI().getAcceptAllFileFilter(this)); setFileFilter((FileFilter) choosableFilters.get(0)); } /** * Returns the 'accept all' file filter from the UI delegate. * * @return The 'accept all' file filter. */ public FileFilter getAcceptAllFileFilter() { return getUI().getAcceptAllFileFilter(this); } /** * Returns the flag that controls whether or not the 'accept all' file * filter is included in the list of filters. * * @return A boolean. * * @see #setAcceptAllFileFilterUsed(boolean) */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -