所有类


javax.swing
类 JFileChooser

java.lang.Object
  继承者 java.awt.Component
      继承者 java.awt.Container
          继承者 javax.swing.JComponent
              继承者 javax.swing.JFileChooser
所有已实现的接口:
ImageObserver, MenuContainer, Serializable, Accessible

public class JFileChooser
   
   
   
   
   
extends JComponent
implements Accessible

JFileChooser 为用户选择文件提供了一种简单的机制。有关使用 JFileChooser 的更多信息,请参阅 《The Java Tutorial》 中的 How to Use File Choosers 一节。

以下代码弹出一个针对用户主目录的文件选择器,其中只显示 .jpg 和 .gif 图像:

    JFileChooser chooser = new JFileChooser();
// Note: source for ExampleFileFilter can be found in FileChooserDemo,
    // under the demo/jfc directory in the JDK.
    ExampleFileFilter filter = new ExampleFileFilter();
    filter.addExtension("jpg");
    filter.addExtension("gif");
    filter.setDescription("JPG & GIF Images");
    chooser.setFileFilter(filter);
    int returnVal = chooser.showOpenDialog(parent);
    if(returnVal == JFileChooser.APPROVE_OPTION) {
       System.out.println("You chose to open this file: " +
            chooser.getSelectedFile().getName());
    }
 


嵌套类摘要
protected  classJFileChooser.AccessibleJFileChooser
          此类实现对 JFileChooser 类的可访问性支持。
 
从类 javax.swing.JComponent 继承的嵌套类/接口
JComponent.AccessibleJComponent
 
从类 java.awt.Container 继承的嵌套类/接口
Container.AccessibleAWTContainer
 
从类 java.awt.Component 继承的嵌套类/接口
Component.AccessibleAWTComponent, Component.BltBufferStrategy, Component.FlipBufferStrategy
 
字段摘要
static StringACCEPT_ALL_FILE_FILTER_USED_CHANGED_PROPERTY
          标识是否使用 AcceptAllFileFilter。
protected  AccessibleContextaccessibleContext
           
static StringACCESSORY_CHANGED_PROPERTY
          声明正在使用不同的 accessory 组件(例如预览文件)。
static StringAPPROVE_BUTTON_MNEMONIC_CHANGED_PROPERTY
          标识用于确认(yes、ok)按钮的助记符的更改。
static StringAPPROVE_BUTTON_TEXT_CHANGED_PROPERTY
          标识确认(yes、ok)按钮上的文本更改。
static StringAPPROVE_BUTTON_TOOL_TIP_TEXT_CHANGED_PROPERTY
          标识用于确认(yes、ok)按钮的工具提示文本的更改。
static intAPPROVE_OPTION
          选择确认(yes、ok)后返回该值。
static StringAPPROVE_SELECTION
          指示确认当前的选择(与按下 yes 或 ok 相同)。
static intCANCEL_OPTION
          选择 cancel 后的返回值。
static StringCANCEL_SELECTION
          指示取消当前的选择。
static StringCHOOSABLE_FILE_FILTER_CHANGED_PROPERTY
          标识预定义文件过滤器列表的更改,用户可从该列表中进行选择。
static StringCONTROL_BUTTONS_ARE_SHOWN_CHANGED_PROPERTY
          指示显示控制按钮。
static intCUSTOM_DIALOG
          指示 JFileChooser 支持特定于开发人员文件操作的类型值。
static StringDIALOG_TITLE_CHANGED_PROPERTY
          标识对话框标题的更改。
static StringDIALOG_TYPE_CHANGED_PROPERTY
          标识显示文件类型的更改(仅显示文件、仅显示目录或显示文件和目录)。
static intDIRECTORIES_ONLY
          指示仅显示目录。
static StringDIRECTORY_CHANGED_PROPERTY
          标识用户的目录更改。
static intERROR_OPTION
          发生错误后返回该值。
static StringFILE_FILTER_CHANGED_PROPERTY
          用户更改要显示的文件种类。
static StringFILE_HIDING_CHANGED_PROPERTY
          标识在显示隐藏文件属性中的更改。
static StringFILE_SELECTION_MODE_CHANGED_PROPERTY
          标识在选择种类(选择单个、多个等等)方面的更改。
static StringFILE_SYSTEM_VIEW_CHANGED_PROPERTY
          声明使用其他对象在系统上查找可用的驱动器。
static StringFILE_VIEW_CHANGED_PROPERTY
          声明使用其他对象检索文件信息。
static intFILES_AND_DIRECTORIES
          指示显示文件和目录。
static intFILES_ONLY
          指示仅显示文件。
static StringMULTI_SELECTION_ENABLED_CHANGED_PROPERTY
          允许选择多个文件。
static intOPEN_DIALOG
          指示 JFileChooser 支持 "Open" 文件操作的类型值。
static intSAVE_DIALOG
          指示 JFileChooser 支持 "Save" 文件操作的类型值。
static StringSELECTED_FILE_CHANGED_PROPERTY
          标识用户单个文件选择的更改。
static StringSELECTED_FILES_CHANGED_PROPERTY
          标识用户多个文件选择的更改。
 
从类 javax.swing.JComponent 继承的字段
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
从类 java.awt.Component 继承的字段
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
从接口 java.awt.image.ImageObserver 继承的字段
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
构造方法摘要
JFileChooser()
          构造一个指向用户默认目录的 JFileChooser
JFileChooser(File currentDirectory)
          使用给定的 File 作为路径来构造一个 JFileChooser
JFileChooser(File currentDirectory, FileSystemView fsv)
          使用给定的当前目录和 FileSystemView 构造一个 JFileChooser
JFileChooser(FileSystemView fsv)
          使用给定的 FileSystemView 构造一个 JFileChooser
JFileChooser(String currentDirectoryPath)
          构造一个使用给定路径的 JFileChooser
JFileChooser(String currentDirectoryPath, FileSystemView fsv)
          使用给定的当前目录路径和 FileSystemView 构造一个 JFileChooser
 
方法摘要
 booleanaccept(File f)
          如果应该显示该文件,则返回 true。
 voidaddActionListener(ActionListener l)
          向文件选择器添加一个 ActionListener
 voidaddChoosableFileFilter(FileFilter filter)
          向用户可选择的文件过滤器列表添加一个过滤器。
 voidapproveSelection()
          用户单击 Approve 按钮(默认情况下标有 "Open" 或 "Save")时由 UI 调用此方法。
 voidcancelSelection()
          用户选择 Cancel 按钮时由 UI 调用此方法。
 voidchangeToParentDirectory()
          将要设置的目录更改为当前目录的父级。
protected  JDialogcreateDialog(Component parent)
          创建并返回包含 this 的新 JDialog,在 parent 窗体中的 parent 上居中。
 voidensureFileIsVisible(File f)
          确保指定的文件是可见的,不是隐藏的。
protected  voidfireActionPerformed(String command)
          通知对此事件类型感兴趣的所有侦听器。
 FileFiltergetAcceptAllFileFilter()
          返回 AcceptAll 文件过滤器。
 AccessibleContextgetAccessibleContext()
          获取与此 JFileChooser 关联的 AccessibleContext。
 JComponentgetAccessory()
          返回 accessory 组件。
 ActionListener[]getActionListeners()
          返回在此文件选择器上注册的所有操作侦听器的数组。
 intgetApproveButtonMnemonic()
          返回确认按钮的助记符。
 StringgetApproveButtonText()
          返回 ApproveButton 中的 FileChooserUI 内使用的文本。
 StringgetApproveButtonToolTipText()
          返回 ApproveButton 中使用的工具提示文本。
 FileFilter[]getChoosableFileFilters()
          获得用户可选择的文件过滤器列表。
 booleangetControlButtonsAreShown()
          返回 controlButtonsAreShown 属性的值。
 FilegetCurrentDirectory()
          返回当前目录。
 StringgetDescription(File f)
          返回文件描述。
 StringgetDialogTitle()
          获得 JFileChooser 的标题栏中所显示的字符串。
 intgetDialogType()
          返回此对话框的类型。
 booleangetDragEnabled()
          获得 dragEnabled 属性的值。
 FileFiltergetFileFilter()
          返回当前选择的文件过滤器。
 intgetFileSelectionMode()
          返回当前的文件选择模式。
 FileSystemViewgetFileSystemView()
          返回文件系统视图。
 FileViewgetFileView()
          返回当前的文件视图。
 IcongetIcon(File f)
          返回此文件或文件类型的图标,这取决于系统。
 StringgetName(File f)
          返回文件名。
 FilegetSelectedFile()
          返回选中的文件。
 File[]getSelectedFiles()
          如果将文件选择器设置为允许选择多个文件,则返回选中文件的列表。
 StringgetTypeDescription(File f)
          返回文件类型。
 FileChooserUIgetUI()
          获得实现此组件 L&F 的 UI 对象。
 StringgetUIClassID()
          返回一个指示 L&F 类名的字符串,该类负责呈现此组件。
 booleanisAcceptAllFileFilterUsed()
          返回是否使用 AcceptAll FileFilter
 booleanisDirectorySelectionEnabled()
          方便的调用,可根据当前的文件选择模式确定目录是否为可选择的。
 booleanisFileHidingEnabled()
          如果在文件选择器中不显示隐藏文件,则返回 true;否则返回 false。
 booleanisFileSelectionEnabled()
          方便的调用,可根据当前的文件选择模式确定文件是否为可选择的。
 booleanisMultiSelectionEnabled()
          如果可以选择多个文件,则返回 true。
 booleanisTraversable(File f)
          如果可以返回该文件(目录),则返回 true。
protected  StringparamString()
          返回此 JFileChooser 的字符串表示形式。
 voidremoveActionListener(ActionListener l)
          从文件选择器中移除一个 ActionListener
 booleanremoveChoosableFileFilter(FileFilter f)
          从用户可选择的文件过滤器列表中移除一个过滤器。
 voidrescanCurrentDirectory()
          通知 UI 重新扫描当前目录的文件列表。
 voidresetChoosableFileFilters()
          将可选择文件过滤器列表重置为其开始状态。
 voidsetAcceptAllFileFilterUsed(boolean b)
          确定是否将 AcceptAll FileFilter 用作可选择过滤器列表中一个可用选项。
 voidsetAccessory(JComponent newAccessory)
          设置 accessory 组件。
 voidsetApproveButtonMnemonic(char mnemonic)
          使用字符设置确认按钮的助记符。
 voidsetApproveButtonMnemonic(int mnemonic)
          使用数值键代码设置确认按钮的助记符。
 voidsetApproveButtonText(String approveButtonText)
          设置 FileChooserUI 中的 ApproveButton 内使用的文本。
 voidsetApproveButtonToolTipText(String toolTipText)
          设置 ApproveButton 中使用的工具提示文本。
 voidsetControlButtonsAreShown(boolean b)
          设置属性,指示在文件选择器中是否显示 approvecancel 按钮。
 voidsetCurrentDirectory(File dir)
          设置当前目录。
 voidsetDialogTitle(String dialogTitle)
          设置显示在 JFileChooser 窗口标题栏的字符串。
 voidsetDialogType(int dialogType)
          设置此对话框的类型。
 voidsetDragEnabled(boolean b)
          设置 dragEnabled 属性,要在此组件上启用自动拖动处理(drag 和 drop 的第一部分),此属性必须为 true
 voidsetFileFilter(FileFilter filter)
          设置当前文件过滤器。
 voidsetFileHidingEnabled(boolean b)
          设置是否实现文件隐藏。
 voidsetFileSelectionMode(int mode)
          设置 JFileChooser,以允许用户只选择文件、只选择目录,或者可选择文件和目录。
 voidsetFileSystemView(FileSystemView fsv)
          设置为访问和创建文件系统资源(如查找软驱和获得根驱动器列表),JFileChooser 所使用的文件系统视图。
 voidsetFileView(FileView fileView)
          设置用于检索 UI 信息的文件视图,如表示文件的图标或文件的类型描述。
 voidsetMultiSelectionEnabled(boolean b)
          设置文件选择器,以允许选择多个文件。
 voidsetSelectedFile(File file)
          设置选中的文件。
 voidsetSelectedFiles(File[] selectedFiles)
          如果将文件选择器设置为允许选择多个文件,则设置选中文件的列表。
protected  voidsetup(FileSystemView view)
          执行公共的构造方法初始化和设置。
 intshowDialog(Component parent, String approveButtonText)
          弹出具有自定义 approve 按钮的自定义文件选择器对话框。
 intshowOpenDialog(Component parent)
          弹出一个 "Open File" 文件选择器对话框。
 intshowSaveDialog(Component parent)
          弹出一个 "Save File" 文件选择器对话框。
 voidupdateUI()
          将 UI 属性重置为当前的外观值。
 
从类 javax.swing.JComponent 继承的方法
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
从类 java.awt.Container 继承的方法
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
从类 java.awt.Component 继承的方法
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusUpCycle
 
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

字段详细信息

OPEN_DIALOG

public static final int OPEN_DIALOG
指示 JFileChooser 支持 "Open" 文件操作的类型值。

另请参见:
常量字段值

SAVE_DIALOG

public static final int SAVE_DIALOG
指示 JFileChooser 支持 "Save" 文件操作的类型值。

另请参见:
常量字段值

CUSTOM_DIALOG

public static final int CUSTOM_DIALOG
指示 JFileChooser 支持特定于开发人员文件操作的类型值。

另请参见:
常量字段值

CANCEL_OPTION

public static final int CANCEL_OPTION
选择 cancel 后的返回值。

另请参见:
常量字段值

APPROVE_OPTION

public static final int APPROVE_OPTION
选择确认(yes、ok)后返回该值。

另请参见:
常量字段值

ERROR_OPTION

public static final int ERROR_OPTION
发生错误后返回该值。

另请参见:
常量字段值

FILES_ONLY

public static final int FILES_ONLY
指示仅显示文件。

另请参见:
常量字段值