I have a Swing application with a JList that works with a custom listmodel.
The model has an ArrayList to store the objects used. The situation is as follows:
Upon closing the application, all objects in the listmodel are serialized (in the default way, the class just implements Serializable
), and written to a file via an ObjectOutputStream
. When the application starts up, all objects are read from the file and stored in my custom ListModel
again.
I'm trying to add a feature to let the user import objects too, from a file he specifies. A static method in another class reads all objects from the file, and returns them in an ArrayList
. I then use the for-each loop in my MainForm
class to store each object from the returned ArrayList
in the ListModel
. In the loop, I want to check whether the ListModel
already contains a certain object, but this does not work.
In code, I'm doing the following:
for(MyObject o: readObjects) {
if(!myListModel.contains(o)) //listmodel just calls contains() on its ArrayList
myListModel.addElement(o);
}
However, even when the object is already in the ArrayList (I keep importing objects from the same file), they are still added.
The question is, how come objects are not equal anymore when deserialized, and is there a way to compare them anyway?