Sunday, May 20, 2012

How to add a open file dialog in a Netbeans project

Adding the File Chooser

  1. Choose Window > Navigating > Inspector to open the Inspector window, if it is not open yet.
  2. In the Inspector, right-click the JFrame node. Choose Add From Palette > Swing Windows > File Chooser from the context menu
    GUI Builder Tip: As an alternative to the 'Add From Palette' context menu, you can also drag and drop a JFileChooser component from the Swing Window category of the Palette to the white area of the GUI builder. It 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.
  3. A look in the Inspector confirms that a JFileChooser was added to the form.
  4. Right-click the JFileChooser node and rename the variable to fileChooser.
  5. File Chooser added - Inspector View
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.

Configuring the File Chooser

Implementing the Open Action

  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.");
        }
    } 
  3. Note: Remove the first and last lines of the code snippet that duplicate the existing ones in the source file.
  4. 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.

Implementing a File Filter

Now you add a custom file filter that makes the File Chooser display only *.txt files.
  1. Switch to the Design mode and select the FileChooser in the Inspector window.
  2. In the Properties window, click the elipsis ("...") button next to the File Filter property.
  3. In the File Filter dialog box, select Custom Code from the combobox.
    A screenshot of the combobox open
  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)";
            }
        } 
     
     
    Forwarded from:
    http://netbeans.org/kb/docs/java/gui-filechooser.html 

No comments: