Netbeans/C2/Adding-a-File-Chooser/English
Title of script: Adding a File Chooser to a Java Application
Author: Sindhu
Keywords: file chooser, JFileChooser, JFileChooser Demo, Swing Applications, video tutorial
Resources for "Adding a File Chooser to a Java Application"
|
|
Slide 1
Welcome |
Hello.
|
Slide 2
Lesson Outline |
In this tutorial, we will
|
Slide 3
System Setup |
For this demonstration, I am using the Linux Operating System, Ubuntu v12.04
|
Slide 4
Introduction
|
In this tutorial, we will learn to add a file chooser to a Java Application using the javax.swing.JFileChooser component.
|
Creating the Application
|
Let us first create the Java Application:
Launch the IDE.
|
In the New Project Wizard, select Java >> Java Application >> click Next | Choose the Java category and the Java Application project type.
|
In the Project Name field, type JFileChooserDemo | In the Project Name field, let's type JFileChooserDemo. |
Uncheck the Create Main Class checkbox. | Clear the Create Main Class checkbox. |
Point to / Check the Set as Main Project checkbox | Make sure that the Set as Main Project checkbox is selected. |
Click Finish. | Click Finish. |
Creating the Application Form
|
Here, we will create the JFrame container and add a few components to it.
|
Choose Swing GUI forms >> JframeForm
|
Choose the Swing GUI Forms category and the JFrameForm type.
|
In the class name field, type >> JfileChooserDemo | For Class Name, type JFileChooserDemo. |
In the Package field, type >> jfilechooserdemo.resources >>
Click Finish. |
In the Package field, type jfilechooserdemo.resources.
|
In the Properties window >> select Title property >> type>> Demo Application >> Press Enter | In the Properties window, select the Title property.
|
Focus on Palette manager >> on RHS of IDE | In the Palette, open the Swing Menus category.
|
To add components to the JFrame Form
|
Select the Menu Bar component and drag it to the top left corner of the Jframe. |
In the jframe >> right-click on Edit menu item >>click Delete | Right-click the Edit item of the Menu Bar component.
|
Next let us add a menu item that allows to open the FileChooser from the running application. | |
Click on the Menu Bar in the Design view to keep it selected | Make sure the Menu Bar is selected before you drag another Menu Item here. |
In the palette >> Swing menus category >> select Menu Item >> drag and drop onto File menu item in the jframe | In the Swing Menus category in the Palette, select a new Menu Item (JmenuItem1).
|
In the jframe >> right-click on jMenuItem1 >> choose Change Variable Name | Right click jMenuItem1 in the Design view.
|
Rename item >> Open >> click OK | Rename the item to Open and click OK. |
Click on >> jMenuItem1 >> press space bar | Make sure that the jMenuItem1 is still selected in the Design view.
Press the Space bar to edit the text of the component. |
Type >> Open >> press enter | Change the text to Open and press Enter to confirm. |
Right-click on Open >> choose Events >> Action >> action Performed | Specify the action handler for the Open menu item.
|
Mode changes to Source view | The GUI Builder automatically switches to the source view.
|
Click on Design button on top of workspaceClick on Design button on top of workspace | Let us switch back to the Design view.
|
In the palette >> Swing Menus category >> select Menu Item >> drag and drop below Open menu item in jframe | In the Palette, choose Swing Menus category.
|
Drag jMenuItem1 to a position below the Menu Bar until you notice the dotted orange line below the Open menu item | Notice the orange highlighting that indicates where the JmenuItem1 is going to be placed. |
Right-click on jMenuItem1 >> choose Change Variable Name | Right click jMenuItem1 in the Design View.
|
Type >> Exit >> click OK | Rename the item to Exit and click on OK. |
Click on >> jMenuItem1 >> press space barMode changes to Source view | Make sure that the jMenuItem1 is still selected in the Design View.
|
Press space bar | Press the Space bar to edit the text of the component |
Type >> Exit >> press enter | Change the text to Exit and press Enter to confirm. |
Right-click on Open >> choose Events >> Action >> action Performed | Specify the action handler for the Exit menu item.
|
Mode changes to Source view | The GUI Builder automatically switches to the Source view.
|
Focus on ExitActionPerformed >> in the Navigator/Inspector window on LHS of IDE | The ExitActionPerformed node appears in the Navigator window below the OpenActionPerformed() node. |
choose Window > Navigating > Inspector (or Navigator) | If you cannot view your Navigator,
|
Here, you can see the ExitActionPerformed node appearing above the OpenActionPerformed node. | |
In the Source mode>> under ExitActionPerformed >> type System.exit(0); | To make the Exit menu item work,
|
Click on Design button on top of workspace | Switch back into Design mode. |
In the palette >> Swing Controls category >> select Text Area >> drag and drop onto jframe form | From the Swing Controls category of the Palette, drag a Text Area (JtextArea) onto the form. |
Resize the text area >> click on handlers >> drag to resize | Resize the added component to make room for the text displayed by the File Chooser later.
|
Right-click on text area item >> choose Change Variable Name >> type textarea | Rename the variable as textarea.
|
choose Window > Navigating > Inspector (or Navigator) | If your Navigator window is not open, choose Window, Navigating, Inspector (or Navigator) to open it.
|
In the navigator >> right-click on Jframe node | In the Navigator, right click the Jframe node. |
Choose Add From Palette > Swing Windows > File Chooser | Choose Add From Palette, Swing Windows, and File Chooser from the context menu. |
In the navigator >> focus on jFileChooser item | You can notice in the Navigator that a JfileChooser was added to the form. |
Right-click on jfileChooser node>> choose Change Variable Name >> type fileChooser | Right click the JfileChooser node and rename the variable to fileChooser.
|
We have now added the File Chooser.
| |
Configuring the File Chooser:
implementing the Open Action
|
Click to select the JfileChooser in the Navigator window.
|
Select dialogTitle property >> type This is my open dialog >> press enter | In the Properties window below the Palette,
|
Now switch to the Source mode. | |
Show code snippet in gedit
|
Now, to integrate the FileChooser into your application.
|
Narration only | This example reads the file contents and displays them in the TextArea. |
Highlight line >> getSelectedFile() >> in code snippet | We will now call the FileChooser's getSelectedFile() method to determine which file the user has clicked. |
copy >> paste inside OpenActionPerformed() method
|
I will copy this code onto my clipboard, and in the Source view of the IDE, paste it inside the OpenActionPerformed method. |
If errors >> right-click in source editor >> select >> Fix Imports | If the editor reports errors in your code, right click anywhere in the code and select Fix Imports. |
Switch to deign mode >> select FileChooser in navigator window | Now, let us add a custom file filter that makes the File Chooser display only .txt files. |
Switch to the Design mode >> in the Navigator window >> click on FileChooser | Switch to the design mode and select the FileChooser in the Navigator window. |
Focus on properties window on RHS >> select File Filter property >> click the ellipsis button | In the Properties window, click the ellipsis button next to the File Filter property. |
In the File Filter window >> select Custom code | In the File Filter dialog box, select Custom Code from the combo-box. |
In the text field >> type>> MyCustomFilter() >> click OK | Type new MyCustomFilter() in the text field.
|
To make the custom code work, we will write the MyCustomFilter class.
| |
Show code snippet >> copy >> paste in the source editor below import statements
|
I will copy and paste this code snippet into the source of our class below the import statements.
|
Running the application
|
Right click the JFileChooserDemo project in the Project window, and select Run to start the sample project. |
In the Run Project window >> select jfilechooserdemo.resources.JFileChooserDemo >> click OK | In the Run Project dialog box, select the jfilechooserdemo.resources.JFileChooserDemo main class.
|
In the Demo Application window >> choose File menu >> Open | In the running Demo Application, choose Open in the File menu to trigger the action. |
Select any .txt file on your system to open | Open any text file to display its contents in the text area.
|
The fileChooser displays the contents of the text file. | |
To close >> select File >> Exit | To close the application, select Exit in the File menu. |
Summary
|
In this tutorial, you learnt to,
and
|
Assignment | As an assignment, use the same demo project we have created and add the following features:
|
Show assignment | I have already created a similar assignment, where the filechooser displays the Save option under the File menu,
|
About the Spoken Tutorial Project | Watch the video at the link shown on the screen.
|
Spoken Tutorial Workshops | The Spoken Tutorial Project Team
|
Acknowledgements | Spoken Tutorial Project is a part of the Talk to a Teacher Project.
|
About the contributor | This tutorial has been contributed by IT for Change. Thanks for joining. |