Professional Documents
Culture Documents
Creating the Application Creating the Application Form Adding the File Chooser Configuring the File Chooser Running the Application To complete this tutorial, you need the following software and resources. Software or Resource NetBeans IDE Version Required Version 6.9 or 7.0
5. Click Finish.
4. Click Finish. 5. In the Properties window, enter Demo application for the Title property and press Enter to confirm. To add components to the JFrame form: 1. In the Palette, open the Swing Menus category, select the Menu Bar component and drag it to the left top corner of the JFrame. Note: If you do not see the Palette, select Window > Palette in the main menu or click Ctrl+Shift+8 to open it.
2. Right-click the Edit item of the Menu Bar component and select Delete in the context menu. 3. To add a menu item that allows to open FileChooser from the running application, in the Swing Menus category in the Palette, select a new Menu Item (JMenuItem1), drag it to the Menu Bar, and drop it to the File item of the Menu Bar. Note: Make sure the Menu Bar is selected before dragging another Menu Item there in order to have the latter added to the Menu Bar.
4. Right-click jMenuItem1 in the Design view and choose Change Variable Name from the context menu. Rename the item to Open and click OK.
click OK. 5. 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. 6. Specify the action handler for the Open menu item. Right-click the menu item and choose Events > Action > action Performed from the context menu. The GUI Builder automatically switches to the Source view and a new event handler method named OpenActionPerformed() is generated. The Navigator window should look as follows:
Training
Java Programming Language
Support
Oracle Development Tools Support Offering for NetBeans IDE
Documentation
General Java Development External Tools and Services Java GUI Applications Java EE & Java Web Development Web Services Applications NetBeans Platform (RCP) and Module Development PHP Applications C/C++ Applications Mobile Applications Sample Applications Demos and Screencasts
7. To add a menu item to exit FileChooser from the application being created, switch back into the Design mode, in the Swing Menus category in the Palette, select a Menu Item (JMenuItem1) and drag it to the Menu Bar below the Open menu item. Notice orange highlighting that indicates where the JMenuItem1 is going to be placed.
More
FAQs 8. Right-click jMenuItem1 in the Design view and choose Change Variable Name from the context menu. Rename the item to Exit and Contribute Documentation! click OK. 9. 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 Docs for Earlier Releases to Exit and press Enter to confirm. 10. Specify the action handler for the Exit menu item. Right-click the menu item and choose Events > Action > action Performed from the context menu. The GUI Builder automatically switches to the Source view and a new event handler method is generated which is named ExitActionPerformed().The ExitActionPerformed node appears in the Navigator window below the OpenActionPerformed() node. 11. To make the Exit menu item work, you include the following source into the ExitActionPerformed() method body:
System.exit(0);
12. Switch back into Design mode. From the Swing Controls category of the Palette, drag a Text Area (JTextArea) into the form like shown in the picture below.
13. Resize the added component to make room for the text displayed by the File Chooser later. Rename the variable to textarea. The form should look like the following screenshot:
You have set up a simple Java application as a base for this tutorial. Next you add the actual File Chooser.
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.
1. Click to select the JFileChooser in the Inspector window, and then edit its properties in the Properties dialog box. Change the 'dialogTitle' property to This is my open dialog, press Enter and close the Properties dialog box. 2. Click the Source button in the GUI Builder to switch to the Source mode. To integrate the File Chooser into your application, paste the following code snippet into the existing OpenActionPerformed() method.
private void OpenActionPerformed(java.awt.event.ActionEvent evt) { int returnVal = fileChooser.showOpenDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); try { // What to do with the file, e.g. display it in a TextArea textarea.read( new FileReader( file.getAbsolutePath() ), null ); } catch (IOException ex) { System.out.println("problem accessing file"+file.getAbsolutePath()); } } else { System.out.println("File access cancelled by user."); } }
Note: Remove the first and last lines of the code snippet that duplicate the existing ones in the source file. 3. If the editor reports errors in your code, right-click anywhere in the code and select Fix Imports or press Ctrl+Shift+I. In the Fix All Imports dialog box accept the defaults to update the import statements and click OK. 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.
4. Type new MyCustomFilter() in the text field. Click OK. 5. 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.
class MyCustomFilter extends javax.swing.filechooser.FileFilter { @Override public boolean accept(File file) { // Allow only directories, or files with ".txt" extension return file.isDirectory() || file.getAbsolutePath().endsWith(".txt"); } @Override public String getDescription() { // This description will be displayed in the dialog, // hard-coded = ugly, should be done via I18N return "Text documents (*.txt)"; } }
Note: To learn how to implement smarter, switchable file filters, have a look at the addChoosableFileFilter method.
4. To close the application, select Exit in the File menu. Have a look at other useful Swing windows and dialogs like the ColorChooser or the OptionPane in the GUI Palette. Send Us Your Feedback
Next Steps
Binding Beans and Data in a Desktop Application with NetBeans IDE Internationalizing a GUI Form with NetBeans IDE Java Tutorial: How to use File Choosers
SiteMap About Us Contact Legal By use of this website, you agree to the NetBeans Policies and Terms of Use. 2011, Oracle Corporation and/or its affiliates. Companion Projects: Sponsored by