• Post Reply Bookmark Topic Watch Topic
  • New Topic

Saving Objects as Files  RSS feed

 
Al Swensen
Greenhorn
Posts: 17
Firefox Browser jQuery PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm new here and I have a question that I've been trying to figure out for awhile now. I'm working with saving files and stuff using a GUI and I was wondering how you could just have a "Save" button (like in MS Word, there's a "Save As..." and a regular "Save" menu item). I currently have a Save As option in my program but it would be cool to just have a regular "Save" option so you just have to click that and it uses the current filename if there is one, or otherwise it pops up a file chooser. Thanks for any help!
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello "Man or Animal"-

Welcome to JavaRanch.

On your way in you may have missed that we have a policy on screen names here at JavaRanch. Basically, it must consist of a first name, a space, and a last name, and not be obviously fictitious. Since yours does not conform with it, please take a moment to change it, which you can do right here.

As to your question, if you have a method to save a document, what is the problem in calling it when the Save menu is invoked? That menu should only be active if a file name is available; otherwise it should be greyed out.
 
Al Swensen
Greenhorn
Posts: 17
Firefox Browser jQuery PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I'll try that! Right now I just have the save stuff in my ActionPerformed method, so when they hit the save button it make the file chooser pop up and everything. So basically, just move the actual save code into a method? How would I check to see if a filename is available? I was trying to figure that out but I have no idea.

And sorry about my ame, I noticed just after I posted that there were requirements so I changed it.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's generally A Good Thing to separate the interesting actions the application does from the user interface. We can do this by adding a little family of objects in the Model View Controller pattern or a couple other popular variations.

In short, we'll make your View (the UI and actionPerformed() method) do as little as possible - just tell the Controller that the user has requested an action. The Controller might tell the Model (the real data the user is working with) to save itself or it might tell the view to pop a file chooser.

See if this notation makes sense:

There might be a controller per window in your system, but there could be fewer or more depending on how your pages break down. The model is just whatever objects you are building up with data from the user interface.

Not shown here is how we glue all these bits together so they know about each other in appropriate ways. We can try to turn this into code together if you like the idea.

BTW: I might always keep a SaveAs option available. I like to copy files in my editor by opening the original and saving with a new name. Any chance your users will want to do that kind of thing?
 
Al Swensen
Greenhorn
Posts: 17
Firefox Browser jQuery PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, I see. I actually did that for displaying and updating the data, because otherwise it would've been repeated at least 5 times in the code. It makes sense to keep everything separate, it makes it cleaner too.

I was actually going to have both a "save as" and a regular "save" option to give the user both options.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like you're on track. Keep us posted how it goes. Since you're showing interest in design topics - and spotting the value of good designs - I'll suggest scrolling on down to the OO, UML, etc. forum where we toss this kind of stuff around all the time.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!