Skip to main content

Programming in Java Netbeans - A Step by Step Tutorial for Beginners: Lesson 46

Danson Wachira is a certified Trainer in Computer Science, Information Technology and related studies.

Table of Contents

<< Lesson 45 | Lesson 47 >>

Lesson 46: Saving files using Java Save File dialog box

In the last couple of lessons, we have been discussing how to use Java Open dialog box, how to customize Java Open dialog box and how to actually open a file using Java Open dialog box. In this lesson, we shall discuss how to use Java Save dialog box.

Java Save dialog box, unlike the Java Open dialog box, is used to access various computer locations where user may need to save a file. By clicking File >> Save or File >> Save As in many windows based applications, you can be able to access the Save or Save As dialog box.

programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-46

In this lesson, we’ll use the same JFrame form we have been using over the previous lessons. We want to code for the Save menu option so that when user select Save option, Java Save dialog box will be displayed for saving a file.

We’ll use the same code we had in Java Open dialog box only that we shall change the ShowOpenDialog() method to ShowSaveDialog() method. On the JFrame design view, right click on the Save menu option and choose Event >> Action >> actionPerformed.

programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-46

We need to include the code that will display the Save dialog box for saving files. Copy the code we have in code stub of the Open menu option to the code stub of the Save menu option. Change the ShowOpenDialog() method to ShowSaveDialog() method after you have copied. We are going to copy just part of it as shown below.

Code stub of the Save menu option

FileFilter ft = new FileNameExtensionFilter("Text Files   .txt", "txt");
fc.addChoosableFileFilter(ft);
FileFilter ft2 = new FileNameExtensionFilter("Word Documents   .doc", "doc");
fc.addChoosableFileFilter(ft2);
FileFilter ft3 = new FileNameExtensionFilter("PowerPoint   .ppt", "ppt");
fc.addChoosableFileFilter(ft3);
FileFilter ft4 = new FileNameExtensionFilter("JPEG Images   .jpeg", "jpeg");
fc.addChoosableFileFilter(ft4);
FileFilter ft5 = new FileNameExtensionFilter("Excel Worksheets   .xsl", "xsl");
fc.addChoosableFileFilter(ft5);

int returnVal = fc.showSaveDialog(this);

Up to this point, we can access the Java Save dialog box and actually use file filters to limit the type of files to be saved. Run the form at this point and try to display the Java Save dialog box through File >> Save option.

programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-46
programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-46

Just like we did in the previous lesson, we shall import the textfile package but this time we shall use the WriteTextFile class instead of ReadFile class.

Add the following code at the top your code. The method name may be different depending on how you named the class.

Scroll to Continue

import textfiles.WriteTextFile;

We’ll then create a new object of WriteTextFile class and use the try …catch block to save (write) an updated copy of an original text file. Add the following code still in the code stub of the Save menu option

Code stub of the Save menu option

FileFilter ft = new FileNameExtensionFilter("Text Files   .txt", "txt");
fc.addChoosableFileFilter(ft);
FileFilter ft2 = new FileNameExtensionFilter("Word Documents   .doc", "doc");
fc.addChoosableFileFilter(ft2);
FileFilter ft3 = new FileNameExtensionFilter("PowerPoint   .ppt", "ppt");
fc.addChoosableFileFilter(ft3);
FileFilter ft4 = new FileNameExtensionFilter("JPEG Images   .jpeg", "jpeg");
fc.addChoosableFileFilter(ft4);
FileFilter ft5 = new FileNameExtensionFilter("Excel Worksheets   .xsl", "xsl");
fc.addChoosableFileFilter(ft5);

int returnVal = fc.showSaveDialog(this);
if (returnVal == javax.swing.JFileChooser.APPROVE_OPTION) {
    java.io.File file = fc.getSelectedFile( );
    String file_name = file.toString( );

    try{
         WriteTextFile file_write = new WriteTextFile( file_name ); //Create new object
         String MyText = txtShow.getText();
         file_write.WriteToFile(MyText);   
       }
    catch (java.io.IOException e){ //Handle any error here
         JOptionPane.showMessageDialog(this, e.getMessage());
    }
  }

Run the form now, open a text file through File >> Open, edit the file and then save it through File >> Save and this time you should be able to save the file using Java Save dialog box.

programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-46

We’ll leave Java Open dialog box and Java Save dialog box at this point and focus on another topic still in Java NetBeans programming but very important. In the next lesson, we start on an introduction to Java and Databases.

<< Lesson 45 | Lesson 47 >>

Want to make money online?

Comments

Danson Wachira (author) from Nairobi, Kenya on March 07, 2013:

Welcome DDE, pleasure is all mine, thanks for the visit and comment.

Devika Primić from Dubrovnik, Croatia on March 07, 2013:

Most informative and you taught me more here thanks

ahmad on March 07, 2013:

thanx man.. u save my life.. :)

Related Articles