Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

How do I make save and saveAs

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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());
}
}
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
     
    Marshal
    Posts: 74371
    334
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    You showed up just in time for the waffles! And this tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    reply
      Bookmark Topic Watch Topic
    • New Topic