PDA

View Full Version : Java - default filename.ext in saveDialog


Yakisoba
09-28-2006, 10:28 AM
I have a save dialog box that seems to work except for a couple issues:

1) when the save dialog first opens I want a default filename "testing.txt" to appear in the filename field.
How do I make this happen?
(currently trying "setSelectedFile(new File("FileName"));", but no luck yet)

2)How can I make the file ALWAYS save as .txt by default?

any suggestions are greatly appreciated.

below is the code for my save button:

String messageCont = outTextArea.getText();

/*create file chooser*/
final JFileChooser fc = new JFileChooser();
/*attache the file chooser to the Frame and detect OK or Cancel*/
int returnVal = fc.showSaveDialog(HanoiFrame.this);
//fc.setSelectedFile();
File file = fc.getSelectedFile();
//File file = fc.setSelectedFile(new File("testing.txt"));



if (returnVal == JFileChooser.APPROVE_OPTION) {

try {
/*if OK write the file*/
BufferedWriter output = new BufferedWriter(new FileWriter(file.getPath() + file.getName()));

output.write(messageCont);
output.close();

} catch (IOException ex) {

ex.printStackTrace();
}
} else {

/*cancel*/
return;

}


Thanks.

YaK

Aradon
09-29-2006, 01:19 AM
Now, I haven't done this before, but you may have to use a File Filter such as:

FileNameExtensionFilter (http://java.sun.com/javase/6/docs/api/javax/swing/filechooser/FileNameExtensionFilter.html)

But I don't know how that'll work with the save Dialog.