Example of jfilechooser to save a file




















This example uses a file chooser that has been customized in several ways. Like the original example, the user invokes a file chooser with the push of a button. Here's a picture of the file chooser:.

Create a file chooser instance. The File and String arguments, when present, provide the initial directory. Show a modal dialog containing the file chooser.

Set or get the currently selected files. Multiple selection is not yet implemented by any look and feel. Calling these methods might produce unpredictable results. Set the file selection mode. Set or get whether multiple files can be selected at once. By default, a user can choose only one file. The component should have a preferred size that looks good in the file chooser. The file chooser fires a property change event when the user selects an item in the list.

A program with an accessory component must register to receive these events to update the accessory component whenever the selection changes. In the example, the ImagePreview object itself registers for these events. This keeps all the code related to the accessory component together in one class.

Here is the example's implementation of the propertyChange method, which is the method called when a property change event is fired:. The loadImage and repaint methods use the File object to load the image and repaint the accessory component.

This table shows the examples that use file choosers and points to where those examples are described. All rights reserved. Hide TOC. Using Swing Components. Try this: Compile and run the example, consult the example index.

Click the Open a File button. Navigate around the file chooser, choose a file, and click the dialog's Open button. Use the Save a File button to bring up a save dialog. Try to use all of the controls on the file chooser. In the source file FileChooserDemo. Then compile and run the example again. You will only be able to see and select directories, not ordinary files. Creates a file chooser instance.

The File and String arguments, when present, provide the initial directory. Shows a modal dialog containing the file chooser. Sets or obtains the currently selected file or if directory selection has been enabled directory. Sets or obtains the currently selected files if the file chooser is set to allow multiple selection. Sets or obtains the file selection mode. Interprets whether directories or files are selectable according to the current selection mode. Sets or interprets whether multiple files can be selected at once.

By default, a user can choose only one file. Sets or obtains whether the AcceptAll file filter is used as an allowable choice in the choosable filter list; the default value is true. Dialog createDialog Component. Given a parent component, creates and returns a new dialog that contains this file chooser, is dependent on the parent's frame, and is centered over the parent.

Sets or obtains the directory whose files are displayed in the file chooser's list. Sets or obtains the property that determines whether automatic drag handling is enabled. See Drag and Drop and Data Transfer for more details.

JFileChooser is a part of java Swing package. JFC contains many features that help in building graphical user interface in java. Java Swing provides components such as buttons, panels, dialogs, etc. JFileChooser is a easy and an effective way to prompt the user to choose a file or a directory. In this article we will see how to use JFileChooser in java swing.

Constructors of JFileChooser are : 1. User should set the path according to their need. Practical Applications of JFileChooser. Skip to content. Change Language.



0コメント

  • 1000 / 1000