Update main form after another form is closed - java

I'm working on an application which is coded in Java. It reads calibration values for later calculations from .xml file on first initialization. Values can be later modified and saved to an xml file from another form called 'Properties', that is opened from the Main form.
How can I notify the Main form after the 'Properties' form is closed to read new modified values from .xml file? Or is there a better way to exchange this data and I should not read .xml again after?

Without more information about how your forms are set up it's hard to give a true answer.
Create an object set up to hold the information you will read from the .xml file. In Main, have one (or more as necessary) of these objects. Store the information from the .xml file into it. Pass and update that data object in Properties form, and have it be returned. After you're done editing, fire a propertyChange or call a method which will trigger tell Main to read from the data store object.
If you want to stick with re-reading the .xml file upon closing the Properties form, then you would need to edit the window/form closing procedure (depends on how your form is set up). If the Properties form is a JFrame, then there should be a "windowClosing" method that can be written for it.

Related

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.

Downloading a file from MySQL using Java EE and primefaces

Well I have a database with a table that contains a BLOB field, the thing is that I need to make a link to download the files.
So far in my backing beans I have a method to generate each record with its respective file, and a method that reads the array of data.
When I use the P:Datatable to show the records I can view every single record, name and file accesing the file.methods in each one.
But I cannot find a way to make it downloadable, I has been searching the forum and the network for away to make something like this
(listadoArticulos.listado is the array of articles, each article contains a few String fields and a single blob)
I need to link to the file itself so the user can download it.
In my backing beans I need to make a method so I can link it, the method has to pass the "File" in the database to bytes (I guess)
I will apreciate any help or direction
You don't use JSF to generate the file download. The file download is done directly by a Java handler/servlet, which will set the content-type directly and stream the file content as bytes.

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

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

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