Thursday, 13 March 2014

FileDialog Box

Actually, you have already written code to open a file. ......

Open FileDialog Box

Actually, you have already written code to open a file. The process is no different for the open file dialogue box. And because you have already written the classes that open and write to a file, you can just import them into the current project.To import a class that you have already written, have a look at the Properties window on the left of NetBeans. (If you can't see the properties window, click the Window menu item at the top of NetBeans. From the Window menu, select Properties.)

Expand the entry for your current project, and right-click the Libraries item:























From the menu that appears, select Add Project. When you do, you'll see the following dialogue box




















Make sure that the Look in box at the top says "NetBeans Projects". Now scroll down and locate your TextFiles project. This is where your classes are that open and write to a file. Now click the "Add project JAR files" button. The dialogue box will close and you'll be returned to NetBeans. Have a look at the Libraries entry for your current project and you'll see that it has been imported:















The Save File Dialogue Box



If you want to display the Save File dialogue box instead of the Open File dialogue box then you can use your File Chooser again. This time, instead of showOpenDialog you use 
showSaveDialog:

int returnVal = db.showSaveDialog( this );

Again, this is enough to display the dialogue box. You don't have to do anything else, though you can add a file filter, as well.

FileFilter ft = new FileNameExtensionFilter( "Text Files", "txt" );
db.addChoosableFileFilter( ft );

int returnVal = db.showSaveDialog(this);

To write the file, add an import statement to the top of your code:

import textfiles.WriteFile;

No comments:

Post a Comment