Adding File Chooser using NetBeans

File chooser is one of the most prominent and widely used feature, used in in any kind of application.This feature can be added to JAVA based application using NeatBeans IDE with few steps and some custom code.This post assumes that you know your way around with NetBeans.

1. Open the file in DESIGN mode.
2. Open Inspector window, if it is not opened. (Windows –> Navigating –> Windows)
3. Add File Chooser :
                 i)Right Click on the JAVA file name in the Inspector.
                 ii)Invoke Add from Palette –> Swing Windows –> File Chooser.

             
4. Right click on the file chooser and change the variable name to “fileChooser”.
5. Now add a jButton using the designer and invoke Events –> Actions –> actionPerformed.
6. Add following code :

int returnVal = fileChooser.showOpenDialog(this.getFrame());
if (returnVal == JFileChooser.APPROVE_OPTION)
{
file2save = fileChooser.getSelectedFile();
// custom code …
}
else {
}

7. Done !! 🙂

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *