• Post Reply Bookmark Topic Watch Topic
  • New Topic

save my application

 
Mohamad Samy
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please help me in that
i know jfilechooser is only to navigate through files and object serialozation used to save objects
what i need is how to save my application as use them later by double clicking any application i save like excel sheet i can save many sheets so i can open any one by double click.
so please give me exampl esaving like this for my applicaton using jfilechoser.
or provide me with any example to study but don't direct me to oracle documents as i have been there for week day and night.
 
Piet Souris
Rancher
Posts: 1635
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mohamad,

I'm not sure I understand your question, but there may be some misunderstanding here.

A JFileChooser only supplies you a name (and possibly the file extension, like '.txt' or '.jpg')
under which your contents will be saved. To get the actual
content saved, well, that's quite another story. It is up to you to write out the contents to be saved,
and how you must do that very much depends on the type of content.

If you want to output simple text, you can do that by using FileWriters, PrintWriters, et cetera.
If you have objects that you want to save, then there are several options: use an object output
stream, output them in <xml> format, whatever you can come up with.

And if you want to output your contents such that you create an 'excel' file, ehh... you better
start studying how the format of an excel file is. Believe me, that could take you many years to
master!

So if you want concrete help, you must give us a concrete example of what it is you are
trying to save.

And yes, chances are that we must direct you to the oracle documentation!

Greetings,
Piet
 
Mohamad Samy
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Here is my program which is a jtable contains some components like jcomboboxes and labels and some buttons to add and delete rows.
my question is if I want to save the entire table with extension like table.mad for example so when I saved it like this I could also just double click that table to open contain my data and I can save many table as I can.
I tried jfilechooser with object serialization using objectoutputstream but no name appeared in the file name in the file chooser and nothing happened so, help me please
 
Piet Souris
Rancher
Posts: 1635
36
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mohamad,

if it is about saving your data to a file, I'll give you a quick emergency rescue plan, so that you'll
be able to save your work. But that will not be the end of my answer!

So here goes:

1) add a button "save" to your panel

2) in the ActionListener for this button, open a JFileChooser dialog.
Set a default name before opening

3) you should get back a File f. Now, create a PrintWriter, as follows:



4) now, suppose your table has 5 columns and a couple of rows. You now proceed as follows:



If some cell happens to be a String, then the 'toString' method is unnecessary.
Note that the code given above may need some slight modification (especially the part that retrieves the cell contents!)

6) Now, what you have done now is to create a so called 'CSV' file. So open up your Windows Explorer, or the Linux/Mac equivalents,
and change the file type to 'CSV'.

The file just created, can be opened and inspected by every text editor in the world, such as Notepad, Word, LibreOffice, excel.

But note: this is a quick and rather dirty solution.

/**********************************************************/

If an important part of your job is to create and save data, then you have to ask yourself two questions:

1) given the nature of the created data, in what format do I want the data to be saved?

For instance: do you want to save it in a 'dbf' format, or 'xml', or plain ascii, or whatever? One of the
things to think of is that you may also want to read the data file into your Java program. Or maybe your company or
the customer demands a certain file format.

2) given an answer to this question, how do I get my program to actually save the data in this format?

Now, data file formats and data saving are huge topics, about which thousands of books must have been written.
You have no choice other than to delve into this matter, if you need it for your work or hobby.

I would certainly advise you to start reading the Oracle docs:

http://docs.oracle.com/javase/tutorial/essential/io/index.html

/******************************************************************/

Well, I hope you'll manage to save your data!

Greetings,
Piet
 
Mohamad Samy
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks will not be enough. you have done a great help for me. that is why i was recommended with this forum.
but a question. i want to save the entire table not just the included data .so when i open this table it keeps the last state of the application with my own extension i left it on. is this a valid question ?
 
Piet Souris
Rancher
Posts: 1635
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mohamad,

yes a valid question.

Now I have two problems:

1) I have actually no expertise with ObjectOutputStreams (OOS for short). Never used them and never will, for the following reason

2) The format of the data that are saved by OOS is very specific to Java (or maybe even to Java 7). That means that if you have
your file with your saved data, and you have no Java available, chances are big that you will not be able to read your own data!
That I find such a major problem, that I, as said, will never use this OOS to save my precious data.

The example that I gave (saving as CSV) means that your data will always be readable, by a great many programs, so you will
always be able to use your data, even without Java. But as I said, I'm not an expert in this area, so I hope that some reader
who knows much more on this subject will step in and tell us more about it.

Before getting back to the OOS, I would state that it is not necessary to save a complete table all at once.
If you have saved the data the way I described, then it should be fairly easy to reconstruct the table.

For instance, if you read in the first line , possibly by using a BufferedReader, then you know that you have read the header of your table.
Then you can say:


and believe it or not, you have your header for your JTable!

and for the other lines you could do something like:


and now fields is all the data for your table. It's as easy as that.

The big problem with the foregoing is of course that in such a plain text CSV file, you lose all
the information about the specific type of your data fields. As I presented it here, it works perfectly
if all your data are of String type.

When saving the data in the way I described, you could write as the very first line something like
Integer,String,Boolean,String,Date
(indicating that the first column is of type Integer, the second of type String, et cetera)

and when reading this first line, you would know what data types your columns should have. This makes
it a bit ugly, but the point is: it is still reasonably easy, and quick to implement.

If you want to save your data in a similar way, but keeping the type of all the data,, then you
must save your data in, say, a DBF format, or in <xml>.
But the problem with that is that that would involve a lot of expertise, or finding libraries that would make
this easy. As said, I hope that some reader with much more expertise in this area will step in.

Now coming back to OOS.
I just looked up that a JTable is serializable, so in theory you should be able to save the whole thing in one go.
I also looked in the Java API:

http://docs.oracle.com/javase/7/docs/api/java/io/ObjectOutputStream.html

They give an example there:



Now, I would say that you should be able to simply put:



and for reading back you do the same, using ObjectInputStream.
If I understand the text of this API correctly, there may be a problem if one of the
data types in your JTable doesn't implement Serializable. What to do then, I'm not sure,
so let's hope that the above code works for you.

Greetings,
Piet
 
Mohamad Samy
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you Piet i also just read some of objectoutputsream and i can try i guess to serialize my entire jframe not only the table using the writeObject method as you mentioned before .
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!