For my chess application, I'd like to be able to save/load positions, so they can be played again... Currently, on my applications' JMenuBar, there is a JMenu with the JMenuItems 'Load' and 'Save'. 'Save' creates a small text file 'output.txt' and saves this in the same directory as the jar file of the application. 'Load' simply looks for the 'output.txt' file in the same directory as the jar file of the application, and reads in the data.
My problem is... The user is forced to save the file in the same directory as the jar file. On a typical menu, under 'File', when you click 'Save As', you are able to browse your hard-drive for a place to save, also when you click 'open' you browse for a file to open. I would like to use the same idea for my application, but I can't figure out how it works exactly.
Have a look at JfileChooser You lucky man, the job is already made.
posted 12 years ago
Hi there, I've got it working, but one little problem when saving files.
Using the code below:
You can see I've set the filter to "txt" files, which works, but it doesn't save them as a text file, it just saves them as type-'File', not type-'Text Document'. This means I have to choose a program to open the file with.
Is there anyway I can save them as text files? Thanks
That's because the file chooser does not automatically append a file extension to the name when you just enter a name without an extension. You'll have to add it yourself in your code, if the user didn't type it in.