Skip to main content

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

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

Table of Contents

<< Lesson 42 | Lesson 44 >>

Lesson 43: Working with Java Open File dialog box

Welcome to Lesson 43 on Programming in Java NetBeans. Over the previous lesson, we discussed how we can implement menus in Java and how we can create shortcuts for menu options. In this lesson, we shall learn the usefulness of Java Open File dialog box and how we can implement this feature in Java NetBeans.

Using Java Open File dialog box, we can access various locations in the computer and open files with ease by way of clicking on a particular file or simply by double clicking the file. Open File dialog box in Java is implemented through File Chooser class control under Java Swing Windows.

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

For this lesson, we’ll create a new project and a new JFrame form. We’ll add a label with title “Working with Java Menus.”

We’ll then add a Java menu with Open, Save and Exit menu options together with their respective shortcuts as we did in the previous lesson (you may need to re-visit Lesson 42) to refresh.

What we need to do is to add a File Chooser on the Open menu option so that when the user clicks on Open, the Open File dialog box will be activated to enable a particular file to be selected. Below is an example of an Open File dialog box from Microsoft Office Word 7.

Microsoft Office Word 7 - Open File Dialog box

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

After you have created the new JFrame form it should look as shown below when run:

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

The Java File Chooser Class Control Under Swing Windows

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

Now, we need to add the File Chooser so that we can be able to activate the Open File dialog box. While you are in design view of the form drag the File Chooser control from the Swing Windows and drop it on the white space near the form but NOT into the form. You will not see any object on the form but you can note an additional object on the Inspector window under “Other components.”

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

We can change the default name of the File Chooser from jFileChooser1 to fc or any other name so, right click on the default jFileChooser1 name on the Inspector window, choose Change Variable Name and from the dialog box that will appear, type fc as the new variable name.

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

Click OK to confirm the renaming changes and the name of File Chooser component in the Inspector Widow will change for the new name.

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

To display the File Chooser dialog box, we use the ShowOpenDialog() method of the File Chooser class. On the form design view, double click on the Open menu option and select Event >> Action >> actionPerformed. This will take you to the code stub of the Open menu option.

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

We need to include the code that will display the dialog box so type the following code in the code stub of the Open menu option.

Scroll to Continue
programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-43

As you can see from the above code, the ShowOpenDialog() method returns an integer value and we are storing this value in a variable called returnVal. This integer value is important to us as we can tell by a number which menu option was clicked i.e. Open, Save or Exit. You will learn how useful this integer value is shortly. Run the form now, select File >> Open and you should be able to display the Open File dialog box.

Java Open File Dialog Box

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

Up to this point, the File Chooser should work but not as expected. Notice that selecting any file and clicking Open does not open that file as expected. To open a file, we need to know which button was clicked by the user, is it “Open” or “Cancel” for example. We’ll use a Swing class called JFileChooser to know which button was clicked by the user. Modify and add the following code in the code stub for Open menu option.

if (returnVal == javax.swing.JFileChooser.APPROVE_OPTION) {

}

The above IF statement will determine if the user clicked one of the approved options. To get the file that was selected by the user, we’ll use the getSelectedFile() method of the File Chooser class. Add the following code still in the code stub for Open menu option.

java.io.File file = fc.getSelectedFile( ); //Type as one line

In the above code, we are getting the file as an object and storing it in a variable named file. Since this file is just an object, we cannot do much with it (like opening or displaying it) unless we change the object into a string. We can change the object into a string value by using the following code.

String file_name = file.toString( );

Now, to display the file that was chosen by the user, we’ll use a JOption Pane dialog box. Add the import statement at the top of your code just before the class.

import javax.swing.JOptionPane;

The code for JOption Pane dialog box should then be placed inside the code stub for Open menu option.

JOptionPane.showMessageDialog( this, file_name);

The complete code in the code stub for Open menu option should now look as follow:

The code in the code stub for Open menu option

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
int returnVal = fc.showOpenDialog(this);
if (returnVal == javax.swing.JFileChooser.APPROVE_OPTION) {
    java.io.File file = fc.getSelectedFile( );
    String file_name = file.toString( );
    JOptionPane.showMessageDialog(this, file_name);
}
}

Run the form again and this time you should have the JOption Pane dialog box displaying the name of the file that was selected by the user.

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

As you can see, we are able to get the file selected by the user through a JOption Pane dialog box. This is very useful if we were to open the file. On the Open dialog box image above, you will notice that File of Type option is set to All Files. We can customize this so that apart from opening all files we can also filter the files to be opened by file extensions. We’ll do this in the next lesson.

<< Lesson 42 | Lesson 44 >>

Want to make money online?

Comments

Benson on February 23, 2013:

Great stuff, i like this!!!!

Danson Wachira (author) from Nairobi, Kenya on February 18, 2013:

Hi Iman,

Thanks for keeping up the follow, am glad that you have benefited from this tutorial. Thanks too for the visit and comment.

Iman on February 18, 2013:

Another great article, i like following this tutorial and usually on look out for new lessons. Thanks for keeping going.

Related Articles