Adding the File Chooser
- Choose Window > Navigating > Inspector to open the Inspector window, if it is not open yet.
- 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.
- A look in the Inspector confirms that a JFileChooser was added to the form.
- Right-click the JFileChooser node and rename the variable to
fileChooser
.
Configuring the File Chooser
Implementing the Open Action
- 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. -
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."); } }
- 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.
Note: Remove the first and last lines of the code snippet that duplicate the existing ones in the source file.
Implementing a File Filter
Now you add a custom file filter that makes the File Chooser display only *.txt files.- Switch to the Design mode and select the FileChooser in the Inspector window.
- In the Properties window, click the elipsis ("...") button next to the File Filter property.
- In the File Filter dialog box, select Custom Code from the combobox.
- Type new MyCustomFilter() in the text field. Click OK.
-
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:
Post a Comment