Filechooser only showing files and not allowing the user to create their own
java, swing, user-interface
Solution
You are using `showOpenDialog` on your JFileChooser. In order to show a Save As tetbox, you probably want to use `showSaveDialog`
public File getFileAddress() {
JFileChooser chooser = new JFileChooser();
//chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = chooser.showSaveDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION) {
return chooser.getSelectedFile();
}
return null;
}
Generally speaking when opening a file, you don't want to let the user create their own file, as this may lead to complications regarding IOExceptions if and when you decide to read from it, thus I assume that it is making that option on OpenDialogs.
To restrict the JFileChooser to certain file tyes, you can use a FileNameExtensionFilter you jut need to put
chooser.setFileFilter(new FileNameExtensionFilter("RTF FIles", ".rtf"));
after your JFileChooser creation.
To ensure this file type on a save file, you will have to manually fix it, uing a bit on String manipulation:
String fileName = chooser.getSelectedFile().getAbsolutePath();
if(!fileName.endWith(".rtf"))fileName += ".rtf";
return new File(fileName);
Problem
Following the oracle tutorial, this code should create a file chooser: ``` public File getFileAddress() { JFileChooser chooser = new JFileChooser(); //chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); int returnVal = chooser.showOpenDialog(this); if(returnVal == JFileChooser.APPROVE_OPTION) { return chooser.getSelectedFile(); } return null; } ``` Which should look like this: Yet, working on a mac, I get this: When what I want to get is this: So how do I get the thing I want with java, since it seems like it's not working.