Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I make save and saveAs  RSS feed

 
charles dexter
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am able to save file onto disc. How do I make Save menu, so that it does not open JFileChooser, if I want to save something already loaded file. I am using netbeans. Here is my code to save a file:

private void mnuSaveAsActionPerformed(java.awt.event.ActionEvent evt) {
// Save the ArrayList and its object with their data as binary file
myChoice = new JFileChooser();
myChoice.showSaveDialog(this);
path = myChoice.getSelectedFile().getPath();
try
{
ObjectOutputStream outstream = new ObjectOutputStream(
new FileOutputStream(path));
outstream.writeObject(myList);

//dataDisplay.append("Array written to disk file;" +path + '\n');
outstream.close();
JOptionPane.showMessageDialog(null,"Your file is saved successfully" );
}
catch(IOException err1)
{
JOptionPane.showMessageDialog(null,"Sorry, " + err1.getMessage());
}
}
 
Chris Nash
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a little inclear on what you want to do. You want to save a file, but not use the JFileChooser dialog. I have two questions:

  • Why would you bring up a dialog to do a Save? Normally the user just hits a key combination, such as Ctrl+S, or chooses a menu item to save a file. Or do you just want a dialog for Save As?
  • Why don't you want to use the JFileChooser? What are you doing that makes it inappropriate?


  • In the future, please use the code tags.
     
    Campbell Ritchie
    Sheriff
    Posts: 53750
    127
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You would have to record your File location probably as a field of your class, and whenever you use save retrieve that File.
    The saveAs method would be allowed to change the File location field.

    You might be able to set up a properties Map and put the File location into that Map.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!