NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

DefaultListModel being updated in another class (by reference?)

 
Post new topic   Reply to topic    NetBeans Forums -> Java EE Users
View previous topic :: View next topic  
Author Message
rickyoswaldiow



Joined: 06 Mar 2009
Posts: 2

PostPosted: Thu Apr 02, 2009 1:57 am    Post subject: DefaultListModel being updated in another class (by reference?) Reply with quote

Hello all, I'm a bit out of my depth on this problem. I think it's somthing to do with the way variables are references when you pass them over to a method in another class, here's the situation:

BRIEF
I have a method that declares an object, set's the Address1 variable of this object, and adds the object to an ArrayList. So, my ArrayList stores all my objects.

I want to display all my objects in a JList, which should automatically update if an object is updated, added or removed. I set up a DefaulListModel and attached it to the JList. If I set it to "watch" my ArrayList, the JList populates with a reference to each object. I want the JList to display somthing nicer; the Address1 String that each object has.

I set up a String array. Any time I update my ArrayList, I also update the String array to store the Address1 string of each object in the ArrayList. Now the DefaultListModel can "watch" the String array.

PROBLEM
This works fine the first time round, in the constructor of my class with the main() method I set up two objects and set their Address1 like so:
Code:

PropertiesList.add(control.AddProperty("moo", null, null, null, null, null, null, null));
PropertiesList.add(control.AddProperty("baa", null, null, null, null, null, null, null));
MainUI mainUI = new MainUI(PropertiesList, control);
mainUI.show();


And the code for the AddProperty method
Code:


    // AddProperty method for Domestic Properties
    public Domestic AddProperty(String m_address1, String m_address2, String m_county, String m_country, String m_postcode, String m_price, String m_picture, String m_bedrooms, boolean m_parking, boolean m_garage, boolean m_garden) {
        Domestic m_property = new Domestic();

        return m_property;
    }

    // AddProperty method for Commercial Properties
    public Commercial AddProperty(String m_address1, String m_address2, String m_county, String m_country, String m_postcode, String m_price, String m_picture, String m_floorspace) {
        Commercial m_property = new Commercial();
        m_property.setAddress1(m_address1);

        return m_property;
    }



In the constructor for MainUI, I go ahead and set up a DefaulListModel. I have it "watch" a String array which is populated using a for loop that copies each Address1 from the ArrayList.
Code:


    /** Creates new form MainUI */
    public MainUI(ArrayList m_properties, Control m_control) {

        initComponents();
       
        this.control = m_control;
        this.PropertiesList = m_properties;
        this.PropertiesListModel = control.GetAllAddress1(PropertiesList, PropertiesAddress);
        this.lstProperties.setModel(this.PropertiesListModel);
    }


Here is the GetAllAddress1 method
Code:


    //
    public DefaultListModel GetAllAddress1(ArrayList m_propertiesList, String[] m_propertiesAddress) {
        DefaultListModel m_propertiesListModel = new DefaultListModel();

        for(int i = 0; i < m_propertiesList.size(); i++) {
            Property temp_property = (Property) m_propertiesList.get(i);
            m_propertiesAddress[i] = (String) temp_property.getAddress1();
            m_propertiesListModel.addElement(m_propertiesAddress[i]);
        }

        return m_propertiesListModel;
    }



+IT WORKS FINE UP TO THIS POINT!+
The JList (lstProperties) on the MainUI has "moo" and "baa" listed. It is when I try to repeat these steps to let a user add a new object that it stops working.
The MainUI also has an "add" button, which opens another JFrame
Code:


AddUI addUI = new AddUI(PropertiesList, PropertiesListModel, PropertiesAddress, control);
addUI.show();


The AddUI class can now reference the DefaultListModel, ArrayList and the String Array?!
Code:


    private ArrayList PropertiesList;
    private DefaultListModel PropertiesListModel;
    private String[] PropertiesAddress;// = new String[99];
    private Control control;
   
    /** Creates new form AddUI */
    public AddUI(ArrayList m_propertyList, DefaultListModel m_propertyListModel, String[] m_propertiesAddress, Control m_control) {
        this.PropertiesList = m_propertyList;
        this.control = m_control;
        this.PropertiesListModel = m_propertyListModel;
        this.PropertiesAddress = m_propertiesAddress;
       
...



Once I type some information into the text fields on the AddUI interface, I commit them by clicking a button
Code:


PropertiesList.add(control.AddProperty(txtAddress1.getText(), null, null, null, null, null, null, null));
//PropertiesListModel.addElement(control.AddProperty(txtAddress1.getText(), txtAddress2.getText(), txtCounty.getText(), txtCountry.getText(), txtPostCode.getText(), txtPrice.getText(), null, null));
PropertiesListModel = control.GetAllAddress1(PropertiesList, PropertiesAddress);



What I am attempting to do here is to add a new object to the ArrayList (PropertiesList) using the AddProperty method again. I then attempt to OVERWRITE the DefaultListModel (PropertiesListModel) using the GetAllAddress1 method again, which returns a DefaultListModel itself. I have observed some interesting behavior here...
The line that is commented out, if uncommented, will update the JList with a reference to the object as many times as I execute the code IF I comment out the line after it.
If both are left uncommented, I can only execute it once with any effect.
There are no error messages when I debug this project.

CONCLUSION
My assumption is, as stated at the begining, that the way I am trying to OVERWRITE the DefaultListModel is where my error lies. Since I do not have a good knowledge of pointers and references, I am not even sure if I'm on the right track! I've uploaded the code here if anyone wishes to take a look (winzip of .java files & netbeans 6.5 project)
http://www.philoswald.f2s.com/EstateAgentPrototype.zip
Back to top
Melongo Annabel
Posted via mailing list.





PostPosted: Thu Apr 02, 2009 11:51 pm    Post subject: DefaultListModel being updated in another class (by reference?) Reply with quote

Ricky,
Have you heard about the Observer and Observable interfaces in Java? I think those are best suited for your implementation:

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Observable.html
http://www.eli.sdsu.edu/courses/fall97/cs535/notes/observer/observer.html
http://www.java2s.com/Code/Java/Design-Pattern/Observableandobserver.htm


From: rickyoswaldiow <address-removed>
To: address-removed
Sent: Wednesday, April 1, 2009 8:57:45 PM
Subject: [nbj2ee] DefaultListModel being updated in another class (by reference?)

Hello all, I'm a bit out of my depth on this problem. I think it's somthing to do with the way variables are references when you pass them over to a method in another class, here's the situation:



BRIEF

I have a method that declares an object, set's the Address1 variable of this object, and adds the object to an ArrayList. So, my ArrayList stores all my objects.



I want to display all my objects in a JList, which should automatically update if an object is updated, added or removed. I set up a DefaulListModel and attached it to the JList. If I set it to "watch" my ArrayList, the JList populates with a reference to each object. I want the JList to display somthing nicer; the Address1 String that each object has.



I set up a String array. Any time I update my ArrayList, I also update the String array to store the Address1 string of each object in the ArrayList. Now the DefaultListModel can "watch" the String array.



PROBLEM

This works fine the first time round, in the constructor of my class with the main() method I set up two objects and set their Address1 like so:


Code:


PropertiesList.add(control.AddProperty("moo", null, null, null, null, null, null, null));

PropertiesList.add(control.AddProperty("baa", null, null, null, null, null, null, null));

MainUI mainUI = new MainUI(PropertiesList, control);

mainUI.show();







And the code for the AddProperty method


Code:




// AddProperty method for Domestic Properties

public Domestic AddProperty(String m_address1, String m_address2, String m_county, String m_country, String m_postcode, String m_price, String m_picture, String m_bedrooms, boolean m_parking, boolean m_garage, boolean m_garden) {

Domestic m_property = new Domestic();



return m_property;

}



// AddProperty method for Commercial Properties

public Commercial AddProperty(String m_address1, String m_address2, String m_county, String m_country, String m_postcode, String m_price, String m_picture, String m_floorspace) {

Commercial m_property = new Commercial();

m_property.setAddress1(m_address1);



return m_property;

}









In the constructor for MainUI, I go ahead and set up a DefaulListModel. I have it "watch" a String array which is populated using a for loop that copies each Address1 from the ArrayList.


Code:




/** Creates new form MainUI */

public MainUI(ArrayList m_properties, Control m_control) {



initComponents();



this.control = m_control;

this.PropertiesList = m_properties;

this.PropertiesListModel = control.GetAllAddress1(PropertiesList, PropertiesAddress);

this.lstProperties.setModel(this.PropertiesListModel);

}







Here is the GetAllAddress1 method


Code:




//

public DefaultListModel GetAllAddress1(ArrayList m_propertiesList, String[] m_propertiesAddress) {

DefaultListModel m_propertiesListModel = new DefaultListModel();



for(int i = 0; i < m_propertiesList.size(); i++) {

Property temp_property = (Property) m_propertiesList.get(i);

m_propertiesAddress[i] = (String) temp_property.getAddress1();

m_propertiesListModel.addElement(m_propertiesAddress[i]);

}



return m_propertiesListModel;

}









+IT WORKS FINE UP TO THIS POINT!+

The JList (lstProperties) on the MainUI has "moo" and "baa" listed. It is when I try to repeat these steps to let a user add a new object that it stops working.

The MainUI also has an "add" button, which opens another JFrame


Code:




AddUI addUI = new AddUI(PropertiesList, PropertiesListModel, PropertiesAddress, control);

addUI.show();







The AddUI class can now reference the DefaultListModel, ArrayList and the String Array?!


Code:




private ArrayList PropertiesList;

private DefaultListModel PropertiesListModel;

private String[] PropertiesAddress;// = new String[99];

private Control control;



/** Creates new form AddUI */

public AddUI(ArrayList m_propertyList, DefaultListModel m_propertyListModel, String[] m_propertiesAddress, Control m_control) {

this.PropertiesList = m_propertyList;

this.control = m_control;

this.PropertiesListModel = m_propertyListModel;

this.PropertiesAddress = m_propertiesAddress;



...









Once I type some information into the text fields on the AddUI interface, I commit them by clicking a button


Code:




PropertiesList.add(control.AddProperty(txtAddress1.getText(), null, null, null, null, null, null, null));

//PropertiesListModel.addElement(control.AddProperty(txtAddress1.getText(), txtAddress2.getText(), txtCounty.getText(), txtCountry.getText(), txtPostCode.getText(), txtPrice.getText(), null, null));

PropertiesListModel = control.GetAllAddress1(PropertiesList, PropertiesAddress);









What I am attempting to do here is to add a new object to the ArrayList (PropertiesList) using the AddProperty method again. I then attempt to OVERWRITE the DefaultListModel (PropertiesListModel) using the GetAllAddress1 method again, which returns a DefaultListModel itself. I have observed some interesting behavior here...

The line that is commented out, if uncommented, will update the JList with a reference to the object as many times as I execute the code IF I comment out the line after it.

If both are left uncommented, I can only execute it once with any effect.

There are no error messages when I debug this project.



CONCLUSION

My assumption is, as stated at the begining, that the way I am trying to OVERWRITE the DefaultListModel is where my error lies. Since I do not have a good knowledge of pointers and references, I am not even sure if I'm on the right track! I've uploaded the code here if anyone wishes to take a look (winzip of .java files & netbeans 6.5 project)

http://www.philoswald.f2s.com/EstateAgentPrototype.zip
Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> Java EE Users All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo