Results 1 to 4 of 4
  1. #1
    rickyoswald is offline Member
    Join Date
    Apr 2009
    Rep Power

    Default DefaultListModel being updated in another class (by reference?)

    First I'd like to say Hello to everyone! This is my first post here, hopefully I'll be able to give back some help to others one day :)

    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:

    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.

    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:
    Java 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);;
    And the code for the AddProperty method
    Java 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();
            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.
    Java Code:
        /** Creates new form MainUI */
        public MainUI(ArrayList m_properties, Control m_control) {
            this.control = m_control;
            this.PropertiesList = m_properties;
            this.PropertiesListModel = control.GetAllAddress1(PropertiesList, PropertiesAddress);
    Here is the GetAllAddress1 method
    Java 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();
            return m_propertiesListModel;

    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
    Java Code:
    AddUI addUI = new AddUI(PropertiesList, PropertiesListModel, PropertiesAddress, control);;
    The AddUI class can now reference the DefaultListModel, ArrayList and the String Array?!
    Java 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
    Java 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.

    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!

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Cambridge, UK
    Rep Power


    If you just overwrite the toString() method of your original object to return Address1, then you can use those items directly with JList. When the JList paints, it will call element.toString() in order to get the Strings to display.

  3. #3
    Triachith is offline Member
    Join Date
    Apr 2009
    Rep Power

    Default Hi! I'm new on this forum..

    Hello every1 :) Just wanted to say hi!
    Been reading this forum for long now and decided to register at last and maybe provide comunity with some helpful info

    Cheers! :)

  4. #4
    Triachith is offline Member
    Join Date
    Apr 2009
    Rep Power

    Default Hi from Florida!

    Just wanted to say hi since I'm new here!
    'm happy to become a member of this community!!!
    Hoping for the the best :)

Similar Threads

  1. Passing Class Reference to method
    By nekt in forum Advanced Java
    Replies: 5
    Last Post: 03-26-2009, 06:08 AM
  2. why my image is not updated ?
    By aneuryzma in forum New To Java
    Replies: 4
    Last Post: 08-15-2008, 12:08 AM
  3. Number of updated records
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-21-2008, 05:32 PM
  4. Updated Forum Rules
    By levent in forum Suggestions & Feedback
    Replies: 1
    Last Post: 08-12-2007, 01:09 PM
  5. Replies: 1
    Last Post: 08-07-2007, 05:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts