I have a function openFileAction()
that is called when I click the 'File' > 'Open' option in my JMenuBar
. Its first lines look like this:
private static String myPath = ... // some path
private void openFileAction() {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File(myPath));
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
File f = null;
try {
fileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
fileChooser.setFileFilter(new FileNameExtensionFilter(null, ".txt");
f = fileChooser.getSelectedFile();
...
I only want to see .txt
files as suggestions -- so I call setFileFilter()
on my fileChooser
.
This works fine for the directory fileChooser
is set to, myPath
-- i.e., in the 'Open' pop-up window that appears, I see only .txt
files (and folders) in that directory. However, if I navigate away from myPath
in the pop-up window, let's say to Desktop
, I see all files (and folders) there, and no longer only the .txt
files, as I would like to.
How can I see only .txt
files in any directory I navigate to?
CodePudding user response:
First, configure the dialog the way you want it, before you show it, so, instead of...
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File(myPath));
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
File f = null;
try {
fileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
fileChooser.setFileFilter(new FileNameExtensionFilter(null, ".txt");
f = fileChooser.getSelectedFile();
You should be doing something more like...
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File(myPath));
fileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
fileChooser.setFileFilter(new FileNameExtensionFilter(null, ".txt");
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
File f = null;
try {
f = fileChooser.getSelectedFile();
Second, configure the FileFilter
correctly. You should be giving it some kind of "description", as this get's presented to the user and you don't need the .
in the extension, instead, it should be more like...
fileChooser.setFileFilter(new FileNameExtensionFilter("Text files", "txt");
Runnable example...
import java.awt.EventQueue;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
File myPath = new File(".");
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(myPath);
fileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
fileChooser.setFileFilter(new FileNameExtensionFilter("Text files", "txt"));
if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
System.out.println("You have selected " selectedFile);
}
}
});
}
}
I'd also consider taking a closer look at How to Use File Choosers