Is it possible to save ComboBox results that uses.addItem(...)? - java

I am considering using the following code:
comboBox.addItem(...);
If I continue using this code to add items to the comboBox, would it be possible to save the items into either a text file or memory? *so that it can be loaded in the next time the user wants to use the gui.
Thanks for any help you may provide. :)

Yes, I would recommend creating a class that serializes the object to a text file. This can be accomplished with Input/Output Streams or the File class.
So you would take the array of objects, then serialize the object to a text file, then read it back into the array the next time the program runs.
Here is some info on Serializable: http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html
File Class: http://docs.oracle.com/javase/7/docs/api/java/io/File.html

Related

Hello, I want to create a javafx project, but does it need Database?

I want to create a javafx project, but does it need Database?
when I create this program save these name when I input this name and other. I mean save the result in the program and show me when I run it, I don’t need to be store for along time .just for that time when I will run it?
Short answer is no, JavaFX can run without database and you don't need to use it.
If you need to sava data that only while program is running but don't need to save it for next use of program then you can simply use some Collection or custom data structure to hold what you need in runtime, but bare in mind that this means that all data will reset once you quit program.
If you need to save data even when program shuts down then you will need to use some kind of database, if you want to save a small amount of data then you can use json, xml or even raw txt files, for more data you should take look at some SQL/NoSQL database.

How to edit json file with data and save it

I have a json file with a lot of objects. This file is located in res/raw. Im reading this file and showing objects in list in my UI. And after user closing activity with this list, changes that user made most be saved in file that was read.
If your file is located inside of your apk file, you cannot change it. Instead of that have a flag stored in persistent store. If it is a first run of your application, then read this json file from apk, process it and store it in the device filesystem.
And every next run, check the flag, if it is not the first run, read the file from the filesystem and process it further.
Here is the bad way of making things static, but if too urgent for you, you can read json file once and convert it to java model(pojo) object and make that object static, When next time you visit same activity load ui from that static object instead of reading and parsing it again, for safety you can check if pojo class object is null.

Plausibility of plan to use DOM4J with JTables & Action Listeners

I am preparing to embark on a large solo project at my place of employment. First let me describe the project. I have been asked to create a Java program that can take a CamT54 file (which is just a xml file) and have java display the information in table form. Then users should be given the ability to remove certain components from the table and have it go back to xml format with the changes.
I'm not well versed in dealing with XML in Java so this is going to be a learn and work task. Before I begin investing time I would like to know that my approach is the best approach.
My plan is to use DOM4J to do the parsing and handling of the xml. I will use a JTable to display the data and incorporate some buttons to the GUI that allow the modifications of the data through the use of some action listeners.
Would this be a plausible plan? Can DOM4J effectively allow xml data to be displayed in a table format and furthermore could that data be easily modified or deleted then resaved to a new xml?
I thought I would go ahead and answer this as I finished the program and wanted to post what I thought was the easiest solution in case anyone else needed help.
It turned out the easiest approach (for me at least) was to use the standard DOM parser, here are the steps I took.
Parsed the entire XML into String array lists. XPath was required for this, I also had to convert the elements into Strings and remove the extra tag information from the string using substrings since I only wanted the actual value.
I populated a JTable with these arrays.
Once users finished editing and clicked a save button then another Dom parser would take the original XML and change each and every attribute using the values from the Arrays (that were deleted and repopulated with the JTable cell values when the user clicked "save").

Update objects written to a text files in java

Writing Java objects or a List into a text file is ok. But I want to know how I can update or rewrite a object which was written previously without writing objects again. For example, let s assume there is a java.util.List has a set of Objects. and then that list is written to a text file. Then later that file will be read again and get all objects from list and then change one object's value at run time by a java application. Then I don't need to write entire list back to the text file. Instead only the updated object in the list is required to be rewritten or updated in the text file without rewriting the whole list again. Any suggestion, or helpful source with sample codes please.
Take a look at RandomAccessFile. This will let you seek to the place in the file you want, and only update the part that you want to update.
Also take a look at this question on stackoverflow.
Without some fairly complex logic, you won't usually be able to update an object without rewriting the entire file. For example, if one of the objects on your list contains a string "shortstring", and you need to update it with string "muchmuchlongerstring", there will be no space in the file for the longer string without rewriting all the following content in the file.
If you want to persist large object trees to a file and still have the ability to update them, your code will be less buggy and life will be simplified by using one of the many file-based DBs out there, like:
SQLite (see Java and SQLite)
Derby
H2 (disk-based tables)

modification of xml file in java

I have a dialog box that the user inserts various data through gui controls,
and this information is saved as an xml file (implemented in java).
The information stored in the xml file is configuration information
for the application.
I can manually modify the xml configuration file, but I also want to provide this capability through a UI as well.
So when the dialog is opened (for creation of configuration) a corresponding well-defined object is populated by the various values input by the user.
Once the user presses 'save' the information in the object is stored as xml.
Now I was thinking to provide the option for edit the file via UI. So the same dialog is displayed to the user, but this time with the configuration information already filled-in by the loaded file. The corresponding object is populated as well.
I am not sure what is the best way to modify the file at this point.
Should I use 2 objects, 1 that stores all the file's info and 1 that stores the modified values from the dialog, and start comparing the objects for changes so that I modify the file? Or should I delete the file and create a new one?
Which is the best approach, and how would I proceed in each?
Thank you
Consider the data flow. The user will work with the GUI and make changes. The moment they make a modification, the data on the GUI is out of step with the XML. If the user opts to save the data then a simple marshalling operation (trivial if using JAXB) will ensure that the XML is updated. You don't need to compare every field, there's no point - of course you have validated the contents prior to committing them to file. If the user opts to cancel then no marshalling takes place.
There is no need to make a backup and no need to compare what is already in the XML.
However, if the user needs to be able to undo a save (such as revert to previous configuration) then you'll need a backup structure (or maintain a stack of GUI models in memory). I would not recommend that approach, though, as you're just chucking in needless complexity. Users are typically happy with a Save or Cancel button and no Revert.
Keep it simple. Just overwrite the entire file using the updated object. Then you won't need any special code for each case. All you will need is one method to marshall the object into the file and one method to un-marshall it.

Categories