1

i need help regarding the save menu item i have in my menu bar. i have set the code to

private void smActionPerformed(java.awt.event.ActionEvent evt) {


}

but i am unsure what needs to go inbetween the brackets? The gui is a form where poeple fill in there medical record e.g combo box "mr/mrs" textfield "medical problems" etc and when they click file save i would like the user to get a save box (like save as box in word) and the information to be saved in a txt file.

P.S. would it be possible to have the "file type" set to save as a txt file as default.

2 Answers2

1

Use JFileChooser#showSaveDialog() to ask the enduser to select a File.

JFileChooser fileChooser = new JFileChooser();
if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
    File file = fileChooser.getSelectedFile();
    // ...
} else {
    // User pressed cancel.
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • cheers mate, will give it a go. been struggling with this for a week thought it would be somthing like – mark philips Nov 30 '10 at 19:20
  • slight problem mate, it shows the save box and the user can click save, but there is no file saved? – mark philips Nov 30 '10 at 19:27
  • The `File` is just an abstract representation. You have to create the file and write some content to the file using basic [Java IO API](http://download.oracle.com/javase/tutorial/essential/io/). Then it'll appear on the local disk file system. This is a completely different problem. Ask a new question if you stucks. – BalusC Nov 30 '10 at 19:29
  • cheers mate, java IO is the next section in the book. will have a look and ask a new question if i have trouble. – mark philips Nov 30 '10 at 19:32
0

Maybe looking at this could help you. I don't fully understand what you need.

http://download.oracle.com/javase/1.4.2/docs/api/javax/swing/JFileChooser.html http://download.oracle.com/javase/tutorial/uiswing/components/filechooser.html

Jeff
  • 103
  • 6