public class LocalStorageExample1 extends Application
LocalStorage
class: loads and saves
a LinkedHashMap
(a HashMap
whose entries have a stable order).
To try it, add some entries to the Map by pressing the "Add Random Entry"
button or by entering key/value strings in the corresponding text fields.
Then save the Map (to a file), clear, and load the saved Map.
with the corresponding buttons.
The map is saved with the LocalStorage
save
method like this:
LinkedHashMap<String, String> map = listModel.getMap(); ApplicationContext.getInstance().getLocalStorage().save(map, "map.xml");And loaded with the
LocalStorage
load
method like this:
Object map = ApplicationContext.getInstance().getLocalStorage().load("map.xml"); listModel.setMap((LinkedHashMap<String, String>)map);The
LocalStorage.save/load
methods can be applied to
anything supported by Java Beans Persistence, i.e. any Java Bean
as well as most of the primitive and utility Java classes.
The LocalStorage.save
method is implemented
with the XMLEncoder
class
and the LocalStorage.load
method with
the XMLDecoder
class.
Take a look at the contents of "map.xml"
by cut and pasting the
complete pathname from the bottom of the GUI into your favorite
text editor.Application.ExitListener
Constructor | Description |
---|---|
LocalStorageExample1() |
Modifier and Type | Method | Description |
---|---|---|
void |
addKeyValueEntry() |
|
void |
addRandomEntry() |
|
void |
clearMap() |
|
void |
loadMap() |
|
static void |
main(java.lang.String[] args) |
|
void |
saveMap() |
|
protected void |
startup() |
Responsible for starting the application; for creating and showing
the initial GUI.
|
addPropertyChangeListener, addPropertyChangeListener, firePropertyChange, firePropertyChange, getPropertyChangeListeners, removePropertyChangeListener, removePropertyChangeListener
addExitListener, end, exit, exit, getContext, getExitListeners, getInstance, getInstance, hide, initialize, launch, quit, ready, removeExitListener, show, shutdown
@Action public void addKeyValueEntry()
@Action public void addRandomEntry()
@Action public void clearMap()
@Action public void loadMap() throws java.io.IOException
java.io.IOException
@Action public void saveMap() throws java.io.IOException
java.io.IOException
protected void startup()
Application
This method is called by the static launch
method,
subclasses must override it. It runs on the event dispatching
thread.
startup
in class Application
Application.launch(java.lang.Class<T>, java.lang.String[])
,
Application.initialize(java.lang.String[])
,
Application.shutdown()
public static void main(java.lang.String[] args)