Java For Complete Beginners - Open File Dialogue Boxes
Java For Complete Beginners - Open File Dialogue Boxes
Home
Programming Course
| 1-Page Articles
| Android
Programming
The Java File Chooser
| Beginners
Computing
| C# .NET
| Data Analysis -
Pandas
In this lesson, you'll see how to display Open File dialogue
| Excel VBA boxes in Java. This is done with the File Chooser control.
Programming
Go back to Design view. In the NetBeans palette, locate the
| Games
Programming
File Chooser item, which is under Swing Windows:
| Javascript for
Beginners
| Microsoft Access
| Microsoft Word
| Microsoft Excel
| Microsoft Power BI Drag a File Chooser near your form, but not onto it. Drop it
just below the form, in a white area. It won't actually appear
| PHP for Beginners on the form, but you can see it in the Navigator window:
| Python for
Beginners
| Web Design
https://www.homeandlearn.co.uk/java/opening_files.html 1/8
07/07/2024 18:57 java for complete beginners - open file dialogue boxes
But run your programme again. Click File > Open on your
form. You should see a dialogue box appear:
https://www.homeandlearn.co.uk/java/opening_files.html 2/8
07/07/2024 18:57 java for complete beginners - open file dialogue boxes
if (returnVal = =
javax.swing.JFileChooser.APPROVE_OPTION) {
https://www.homeandlearn.co.uk/java/opening_files.html 3/8
07/07/2024 18:57 java for complete beginners - open file dialogue boxes
So the file chosen by the user will end up in the File object
that we've called file.
https://www.homeandlearn.co.uk/java/opening_files.html 4/8
07/07/2024 18:57 java for complete beginners - open file dialogue boxes
This line just uses the toString method of File objects. We're
placing the result in a new variable called file_name. Add the
line to your IF Statement.
javax.swing.JOptionPane.showMessageDialog(FormObjects.this,
file_name);
import javax.swing.JOptionPane;
JOptionPane.showMessageDialog(
FormObjects.this, file_name);
Run your programme and test it out. Click File > Open to see
the dialogue box. Select any file on your computer and click
Open. Your message box should display:
https://www.homeandlearn.co.uk/java/opening_files.html 5/8
07/07/2024 18:57 java for complete beginners - open file dialogue boxes
Before we add the code to open up the selected file, you may
have noticed that Files of Type on your Open file dialogue
box is set to "All files". You can filter the files on this list, so
that the user can open only, say, text files, or just images with
certain extensions (jpeg, gif, png).
import javax.swing.filechooser.FileFilter;
import
javax.swing.filechooser.FileNameExtensionFilter;
You can add more than one extension. Just type a comma
and then the files you want to display:
Once you have a filter object set up, you can use the
addChoosableFileFilter method of your dialogue box:
db.addChoosableFileFilter( ft );
https://www.homeandlearn.co.uk/java/opening_files.html 6/8
07/07/2024 18:57 java for complete beginners - open file dialogue boxes
Add the line to your code, just below the FileFilter line:
Select the Text Files option. Your dialogue box will then only
displays files with the extension .txt.
If you want another line on the list, (to display html files, for
example), you can set up another FileFilter object:
When the programme is run, the Files of Type list would then
look like this:
We can now write the code to actually open the file. We'll do
that in the next part.
https://www.homeandlearn.co.uk/java/opening_files.html 8/8