• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Swing MVC updating JList from another class

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there, I'm trying to make a PhoneBook java application in which every entry is a telephone user that has the following instance fields:
String name, String surname, String Personal Identification Code, Phone nrPhone where Phone is a custom data type created by me which contains only a field: String phoneNo;

The Gui part was made using Free Design from Netbeans. There is a main JFrame Form that contains a JList(where every phoneUser will be displayed and manipulated) and a couple of buttons: Add Person, Delete Person.
Whenever I press on any of these two buttons, another JFrame Window will appear that contains some labels and textfields and also the Add/Delete button. When I press on the Add button from the second GUI, the magic happens: I get the textfields's values and creating a new Person using those values.
On the other side I have the logic part(The Model from Swing MVC that extends AbstractListModel) that communicates with the second JFrame Form class, not the one that contains the JList, you know?
At this moment I'm able to display info in JList from class A only if class A has an instance of the model that extends AbstractListModel. But in my case, there is a second class that communicates with the model(the JFrame Form containing the labels and textfields from which I get the values) and I need to know how to pass that updated model to my main JFrameForm that contains the JList and as a consequence make the JList display the info.
I have to mention I will serialize/deserialize all this such that each time the app is loaded, on the main frame will appear the updated JList which will be serialized to a local file.

Here is the code!


MainGui.java(Main JFrame Form)




Second GUI Class, the one that communicates with the model!



The class phoneUser



the model



This is how I see the things. The model gets updated whenever I press on the add button and it notifies the JList that the info has changed. I pass that model to the MainGui where my JList actually is. Why is it not working?
May it be a problem related to java swing concurrency in the Event Dispatching Thread?

As you can see the JList modifies its size but nothing is displayed in the JList! Please help me! Thanks in advance, have a good day!







 
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alin,
I took a quick glance at your code, and being in a different language always and no headers on your Frame objects puts a damper on things, but this line is making an orphan. You might take a look there to start off.

Les
 
Alin Andrei
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Les Morgan wrote:Alin,
I took a quick glance at your code, and being in a different language always and no headers on your Frame objects puts a damper on things, but this line is making an orphan. You might take a look there to start off.

Les



Well, first of all, thank you very much for your intervention. I found where the problem was and fixed it, now I can display the added persons in the JList but now I have another problem.
This is how the updated classes look like:

MainGui



adaugaPers class updated



model class updated


The problem is this:
I'm saving the object, the LinkedList has the size corresponding to how many elements I added to it. Then, after I deserialize, the persons still appear in the JList but the LinkedList's size is 0. Why is this happening? When serializing, the list looses it's length...Please help me with this too Thanks a lot!!
 
Les Morgan
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alin,
If I understand correctly you are trying to display something in the second form. The problem you are running into, I think, is that the form you want to display something in is an orphan. You do not have a reference to it, so you cannot display something in it. I think you have a reference to an object of that type, but not to the actual form that is being displayed. When you do a "new MyForm.setVisible(true)" you get a new form, and ALL of the components are new inside that form, so it is orphaned--you have no easy way of reaching that form to add anything to it.

If that is not the case, then you need to do some debug coding. First off, put a title on all of your forms, and make the title distinct for the location it is created and displayed. That way you can tell exactly what form you have being displayed. Once you know that, then you will know what is happening or be more able to further debug.

I discount the idea of blocking the EDT causing the form not to display, if the EDT was blocked, then he form would pop up sluggishly and res in sluggishly and may be unresponsive to any actions done upon the frame. I look more for that idea that you are updating an instance of the form, but what is being displayed is an new orphan instance--independent of the instance you update.
Les
 
Alin Andrei
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Les Morgan wrote:Alin,
If I understand correctly you are trying to display something in the second form. The problem you are running into, I think, is that the form you want to display something in is an orphan. You do not have a reference to it, so you cannot display something in it. I think you have a reference to an object of that type, but not to the actual form that is being displayed. When you do a "new MyForm.setVisible(true)" you get a new form, and ALL of the components are new inside that form, so it is orphaned--you have no easy way of reaching that form to add anything to it.

If that is not the case, then you need to do some debug coding. First off, put a title on all of your forms, and make the title distinct for the location it is created and displayed. That way you can tell exactly what form you have being displayed. Once you know that, then you will know what is happening or be more able to further debug.

I discount the idea of blocking the EDT causing the form not to display, if the EDT was blocked, then he form would pop up sluggishly and res in sluggishly and may be unresponsive to any actions done upon the frame. I look more for that idea that you are updating an instance of the form, but what is being displayed is an new orphan instance--independent of the instance you update.
Les



Please, look above. I was modifying the post while you were posting. The updated code is up above. I also titled the GUI's


The problem is after deserializing, the list keeps its length(saved state) but when I try to add another person to the list already populated, it no longer works, like the serialization process created another list after pressing on the "Add" button. So, the list keeps its length after serialization but when I press on Add button it creates another LinkedList...I don't get it!
 
Alin Andrei
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand why, after serialization and pressing the "Add" button, it creates a new list(LinkedList)...suggestive photo:



After pressing the "Add" button it shows me an empty List...
 
Les Morgan
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alin,
You have "lista.setModel(model);" once in the declaration area of MainGui and once where you deserialize, open, but I don't see any where in that Class where lista is defined or where it is initially instantiated. Be aware, that when you deserialize, load, an object that is a new object and not the one that was loaded, so none of your references to it will be correct. Nor can you expect it to take back up where the old one left off. You either have to change a instance variable reference to it, or return a reference to the new object back out of your method. It looks like you are trying to change a instance variable reference, but once again, I don't see any instantiation, or declaration as far as that goes, of "lista" in the object where it is being used.
Les
 
Alin Andrei
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Les Morgan wrote:Alin,
You have "lista.setModel(model);" once in the declaration area of MainGui and once where you deserialize, open, but I don't see any where in that Class where lista is defined or where it is initially instantiated. Be aware, that when you deserialize, load, an object that is a new object and not the one that was loaded, so none of your references to it will be correct. Nor can you expect it to take back up where the old one left off. You either have to change a instance variable reference to it, or return a reference to the new object back out of your method. It looks like you are trying to change a instance variable reference, but once again, I don't see any instantiation, or declaration as far as that goes, of "lista" in the object where it is being used.
Les


Well, lista refers to the JList Swing element not to the LinkedList collection element which is defined and instantiated in the model. So, the JList is being instantiated in the InitComponents method, code generated by NetBeans. I have a problem with the collection type lista which is of type LinkedList. So, the linkedlist keeps its size after deserialization but once I click the "Add" button it seems somewhere in the cod I'm creating a brand new lista object like this: List lista=new LinkedList(); and this indeed creates a new list with an empty size...but how can I avoid this? I think this is the piece of code where the problem occurs.



adaugaPers.java



model.java



Question: After I Press on the "Add" button from the adaugaPers.java class, it also creates a new LinkedList? When I run the app, add the persons then serialize and If I don't close the application, the "Add" button adds elements to the same linkedlist object but once I close the application it creates a brand new object. How can I avoid this?
So, the problem is not related to the JList Swing element also called lista because it's being populated by the model, the model notifies the JList that its info changed. But, once the code creates a new LinkedList object each time I run the application, then there has to be the problem. How can it be solved? I have to instantiate that list, there's no other way. Otherwise those 2 overriden methods of the model won't compilate because I will use a null reference of the linkedlist object.
So this is the current behaviour of the app:

Run the app: MainGui, then press on the Add button which makes the second frame visible and there I'm introducing some values into the textfield, click on Add(aka Adauga), the Jlist Swing element updates itself because the model for it has been set well, then after adding a few persons, I serialize the object
At this time the linkedlist has the size 4, as you can see... 4 persons are displayed in the JList Swing element:



Serialized the object, everything well. Close the app! Call the app back again, deserialize(click on open menuitem), load the saved file and I get this:



Then I will try to manipulate the JList, add, delete persons from it but it creates a new linkedlist object with initial size zero.


As you can see, when I try to add "Gigel", after I press on "Adauga" button, it appears this:
1
model@4fa.... How can I avoid this? Thanks a lot for your help!
 
Les Morgan
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You shouldn't avoid creating a new Linked List when you start, that would probably be a little counter productive, but when you deserialize, you need to replace the existing object with the deserialized objects. That means that you need a reference in the deserializing, loading, routine or you need to return the object to replace the reference with the deserialized object.

So... you have to fix it in the deserializing routine or return the object reference to the deserialized object.

Alin Andrei wrote:
Question: After I Press on the "Add" button from the adaugaPers.java class, it also creates a new LinkedList? When I run the app, add the persons then serialize and If I don't close the application, the "Add" button adds elements to the same linkedlist object but once I close the application it creates a brand new object. How can I avoid this?

 
Alin Andrei
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Les Morgan wrote:You shouldn't avoid creating a new Linked List when you start, that would probably be a little counter productive, but when you deserialize, you need to replace the existing object with the deserialized objects. That means that you need a reference in the deserializing, loading, routine or you need to return the object to replace the reference with the deserialized object.

So... you have to fix it in the deserializing routine or return the object reference to the deserialized object.

Alin Andrei wrote:
Question: After I Press on the "Add" button from the adaugaPers.java class, it also creates a new LinkedList? When I run the app, add the persons then serialize and If I don't close the application, the "Add" button adds elements to the same linkedlist object but once I close the application it creates a brand new object. How can I avoid this?


"but when you deserialize, you need to replace the existing object with the deserialized objects." You mean, I have to find a way to replace the existing linkedlist with the deserialized model.lista linkedlist or the whole model object? Would you mind put a few lines of code? Thanks!
This is the code where I deserialize:


 
Les Morgan
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alin,
It looks like you are already doing what I am suggesting, so that leaves things with a situation that can be remedied with a boolean flag.

Let me see if I have this correct before we go any further:

1 - when you start the program you need to create everything (obviously)
2 - when you start you create the List in question in an ActionListener--click event.
3 - you need to do the same click which activates the same create process as when you startred, so you get a new List that is clear of the data you just deserialized in with the object.

If this is the scenario, then you need a boolean flag that you can set in the serialization process that has object scope--instance variable--so you can encapsulate the create List process in the if clause.

So something like this:

Les
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic