Active 6 years, 9 months ago. Viewed times. I am getting this error: FileChooserDemo3. I have a catch statement.. Thanks for any help! Scanner; import java. This centers the dialog on the screen. Add a comment. Active Oldest Votes. Socratic Phoenix Socratic Phoenix 6 6 silver badges 18 18 bronze badges. It is very bad practice to catch Exception , as this will hide RuntimeExceptions that occur in that block.
Thank you! The instructions say I only need to worry about accepting text files, and then I have to display the txt file info to a textbox in the GUI. I have created the textarea, how do I get the file to display in the textarea?
If you have additional questions on this, I think perhaps you'd be better off clicking the solution I posted as correct, and posting another question with your updated code — ControlAltDel. Sign up or log in Sign up using Google.
Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. The argument to the showOpenDialog method specifies the parent component for the dialog. The parent component affects the position of the dialog and the frame that the dialog depends on.
For example, the Java look and feel places the dialog directly over the parent component. If the parent component is in a frame, then the dialog is dependent on that frame. This dialog disappears when the frame is minimized and reappears when the frame is maximized. By default, a file chooser that has not been shown before displays all files in the user's home directory.
You can specify the file chooser's initial directory by using one of JFileChooser 's other constructors, or you can set the directory with the setCurrentDirectory method. The call to showOpenDialog appears in the actionPerformed method of the Open a File button's action listener:.
The show Xxx Dialog methods return an integer that indicates whether the user selected a file. To get the chosen file or directory, if you set up the file chooser to allow directory selections , call the getSelectedFile method on the file chooser. This method returns an instance of File. The example obtains the name of the file and uses it in the log message. You can call other methods on the File object, such as getPath , isDirectory , or exists to obtain information about the file.
You can also call other methods such as delete and rename to change the file in some way. Of course, you might also want to open or save the file by using one of the reader or writer classes provided by the Java platform. The example program uses the same instance of the JFileChooser class to display a standard save dialog.
This time the program calls showSaveDialog :. By using the same file chooser instance to display its open and save dialogs, the program reaps the following benefits:. Finally, the example program has commented-out lines of code that let you change the file selection mode.
For example, the following line of code makes the file chooser able to select only directories, and not files:. If you want to create a file chooser for a task other than opening or saving, or if you want to customize the file chooser, keep reading. 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 is a picture of the file chooser:. As the figure shows, this file chooser has been customized for a special task Attach , provides a user-choosable file filter Just Images , uses a special file view for image files, and has an accessory component that displays a thumbnail sketch of the currently selected image file.
The remainder of this section shows you the code that creates and customizes this file chooser. See the example index for links to all the files required by this example.
As you have seen, the JFileChooser class provides the showOpenDialog method for displaying an open dialog and the showSaveDialog method for displaying a save dialog. The class has another method, showDialog , for displaying a file chooser for a custom task in a dialog. In the Java look and feel, the only difference between this dialog and the other file chooser dialogs is the title on the dialog window and the label on the approve button.
Here is the code from FileChooserDemo2 that brings up the file chooser dialog for the Attach task:. The first argument to the showDialog method is the parent component for the dialog.
The second argument is a String object that provides both the title for the dialog window and the label for the approve button. Once again, the file chooser doesn't do anything with the selected file. The program is responsible for implementing the custom task for which the file chooser was created. By default, a file chooser displays all of the files and directories that it detects, except for hidden files.
A program can apply one or more file filters to a file chooser so that the chooser shows only some files. The file chooser calls the filter's accept method for each file to determine whether it should be displayed.
A file filter accepts or rejects a file based on criteria such as file type, size, ownership, and so on. Filters affect the list of files displayed by the file chooser.
The user can enter the name of any file even if it is not displayed. JFileChooser supports three different kinds of filtering. The filters are checked in the order listed here. For example, an application-controlled filter sees only those files accepted by the built-in filtering. The preceding code sample uses the getExtension method and several string constants from Utils. In the Java look and feel, the chooser's list shows each file's name and displays a small icon that represents whether the file is a true file or a directory.
You can customize this file view by creating a custom subclass of FileView and using an instance of the class as an argument to the setFileView method. The example uses an instance of a custom class, implemented in ImageFileView. The ImageFileView class shows a different icon for each type of image accepted by the image filter described previously.
The customized file chooser in FileChooserDemo2 has an accessory component. Otherwise, the accessory component is empty.
0コメント