java JFileChooser default name

S

SV

Hello,

I 'm writing a program in which I should be able to open and save files.
The problem is:
when I open a file, then create a new file and try to save this new file,
the save dialog window still suggests the name of that previous file.
This may seem like a detail, but I think you can easily overwrite your
previous file this way ...
So I 'd like to either set a default filename when using "save as", or let
the dialog window suggest no filename at all...

setSelectedFile(null) or setSelectedFile(default) doesn't seem to work ...

Does anyone have an idea how I can do this?

Thanks!

Stephanie
 
A

Alex Hunsley

> Hello,

I 'm writing a program in which I should be able to open and save files.
The problem is:
when I open a file, then create a new file and try to save this new file,
the save dialog window still suggests the name of that previous file.
This may seem like a detail, but I think you can easily overwrite your
previous file this way ...
So I 'd like to either set a default filename when using "save as", or let
the dialog window suggest no filename at all...

setSelectedFile(null) or setSelectedFile(default) doesn't seem to work ...

Does anyone have an idea how I can do this?

Thanks!

Stephanie

Strange. The JavaDocs for JFileChooser say:

getSelectedFile

public File getSelectedFile()

Returns the selected file. This can be set either by the programmer
via setFile or by a user action, such as either typing the filename into
the UI or selecting the file from a list in the UI.

Returns:
the selected file
See Also:
setSelectedFile(java.io.File)


... but I can't find the actual setFile() method mentioned in the docs...

alex
 
Last edited by a moderator:
A

Andrew Thompson

Does the second 'seem' to compile?
....
Strange. The JavaDocs for JFileChooser say: ...
.. but I can't find the actual setFile() method mentioned in the docs...

I think they meant JFileChooser.setSelectedFile(java.io.File)
but made a typo.

OTOH, to the OP. Have you considered setting the selected file to
".", or "new01.txt"?

You can also check for the file's existence when you go to write
it, if it exists, ask the user to confirm.

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.LensEscapes.com/ Images that escape the mundane
 
Last edited by a moderator:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top