Java JFileChooser example with topics on JButton, diifference between AWT and swing, JRadioButton, The object of JFileChooser class represents a dialog window from which the user can select file. It inherits . Computer Network tutorial. The JFileChooser Component is used to create a cross platform directory explorer that can be . Download the Eclipse project of this tutorial. Using JFileChooser: JFileChooser «Swing «Java Tutorial.
|Country:||Trinidad & Tobago|
|Published (Last):||26 July 2010|
|PDF File Size:||13.97 Mb|
|ePub File Size:||8.50 Mb|
|Price:||Free* [*Free Regsitration Required]|
This tutorial shows how to add a file chooser to a Java application using the javax. As part of the exercise, you will create a small Java application that loads a.
You can download the resulting project with a file chooser. Make sure the Menu Bar is selected before dragging another Menu Item there jfilechooserr order to have the latter added to the Menu Bar. You have set up a simple Java application as a base for this tutorial. Next you add the actual File Chooser.
Gutorial will have the same result, but it is a bit harder, because the preview of the JFileChooser is rather big and you might accidentally insert the window into one of the panels, which is not what you want.
SWING JFileChooser Class
You have added a File Chooser. Next you tune the File Chooser to display the title that you want, add a custom file filter, and integrate the File Chooser into your application.
Remove the first and last lines of the code snippet that duplicate the existing ones in the source file. As you can see, you call the FileChooser’s getSelectedFile method to determine which file the user clicked, so you can work with it. This example reads the file contents and displays them in the TextArea.
To learn how to implement smarter, switchable file filters, have a look at the addChoosableFileFilter method.
Choose the Tutlrial category and the Java Application project type. Deselect the Create Main Class checkbox. Creating the Application Form In this section, you create a JFrame container and add a few components to it.
SWING – JFileChooser Class
To create the JFrame form: For Package, tuotrial jfilechooserdemo. In the Properties window, enter Demo application for the Title property and press Enter to confirm. To add components to the JFrame form: Right-click the Edit item of the Menu Bar component and select Delete in the context menu.
Rename the item to Open and click OK. Make sure that the jMenuItem1 is still selected in the Design view. Press the Space bar to edit the text of the component. Change the text to Open and press Enter to confirm. Specify the action jfilechoser for the Open menu item. The Navigator window should look as follows: Notice orange highlighting that indicates where the JMenuItem1 is going to be placed. Rename the item to Exit and click OK. Change the text to Exit and press Enter to confirm.
Specify tuutorial action handler for the Exit menu item. To make the Exit menu item work, you include the following source into the ExitActionPerformed method body: Resize the added component to make room for the text displayed by the Tutorila Chooser later.
Rename the variable to textarea. The form should look like the following screenshot: In the Navigator, right-click the Other Components node. A look in the Navigator confirms that a JFileChooser was added to the form. Right-click the JFileChooser node and rename the variable to fileChooser. Change the dialogTitle property to This is my open dialogpress Enter and close the Properties dialog box.
To integrate the File Chooser into your application, paste the following code snippet into the existing OpenActionPerformed method. Switch to the Design mode and select the fileChooser in the Navigator window. In the Properties window, click the elipsis ” In the File Filter dialog box, select Custom Code from the combobox.
Type the following in the text field: To make the custom code work, you write an inner or outer class MyCustomFilter that extends the FileFilter class. Copy and paste the following code snippet into the source of your class below the import statements to create an inner class implementing the filter.
In the Run Project dialog box select the jfilechooserdemo.
In the running Demo application, choose Open in the File menu to trigger the action. The result should look like this: To close the application, select Exit in the File menu. Send Feedback on This Tutorial. Documentation General Java Development. External Tools and Services.
Docs for Earlier Releases.