Results 1 to 10 of 10
  1. #1
    nikosa is offline Member
    Join Date
    Nov 2008
    Posts
    20
    Rep Power
    0

    Default JComboBox Refresh..

    Hey guys I have a problem with jcombobox

    I have 2 objects

    client and property

    the client object add a client in the system and the new client is allocated an id
    the property object add a property in the system but it is strickly connected with the client because the client own the property.

    Now when the user adds a property he is asked to select the client from a combobox. everything ok so far. when i add a new client in the system and then i try to add a new property i cannot find the client that i have added in the combo box.

    what i want to do is to refresh the combobox in the property object everytime I add a new client i the system. What i really want is to forch the system to query the db in order to get both the name and new id that the client has been allocated.

    I tried to call the panel that holds the combo box but it does not work.

    Any help will be much appreciated...
    Last edited by nikosa; 11-26-2009 at 02:20 PM.

  2. #2
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    Without seeing the code or any idea of what is going on or where, I can only suggest what I'm doing when tying objects and GUI Objects.

    Take a look at the PropertyChangeListener and PropertyChangeEvents. Add a way to throw the events when the object changes, then something listen for the events and refresh the GUI object.

    Sorry without more information of how exactly/ where exactly the problem is this is the best I can do.

  3. #3
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default

    I'm just guessing here, but this afternoon I was using some JComboBoxes and one of them was not refreshing properly because I created it with a model and the new items should be added to the model, instead of the JComboBox. You can also try the method validate()
    Last edited by sky; 11-28-2009 at 06:43 PM.

  4. #4
    nikosa is offline Member
    Join Date
    Nov 2008
    Posts
    20
    Rep Power
    0

    Default

    hello guys,

    sorry for being so late but I was away for a few days.

    I have a project built with about 20 classes so far and I think it is impossible to give you my code let you figure out yourselves what is going on.Even I have problems sometimes ;-) . But I will try a different approach to explain you...

    Well, i am developing an application that the user will be able to add properties (flat, detached, offices, shops) in the system. Everything is going perfectly good until I end up in the location issue. Here is the problem...

    I have a super class named: Property
    I have many childcl asses that extend the Property class named:
    1. Flat
    2. Detached
    3. Office
    4. ...

    The parent class holds all the common attributes that a property have say road name (STRING),
    road number (STRING),
    location (INT),
    floor (INT)...

    the location is int because it holds the id of the location and NOT the name of the location. The location are stored in the database in following form.

    ID - Name
    1 - London
    2 - Paris
    3 - Barchelona

    I run a query that selects the name of the location based on the id that it is stored as a reference in the property's location attribute.

    The idea behind it is that I don't want to let the user type the location of the property because I want to avoid misspelling problems say london, londoon, londonn, etc....

    needless to say every property has an location attribute. this is the reason why I keep the combobox in the parent class.

    public class property{

    protected JTextField roadname, address;
    protected JComboBox locationCB;

    public property(){

    //here i initialize everything..
    locationCB = new JComboBox();
    updateLocations();
    }

    //this is a seperate method because i want to add the locations added by the user dynamically.

    public void updateLocations(){

    locationCB.removeAllItems();
    locationCB.addItems(retrieved from the db);

    }

    }

    Now I have a class

    public class Flat extends Property{

    public Flat(){

    add the locationCB in the flat userinterface

    }

    }

    and another class

    public class Office extends Property{

    public Office(){

    add the locationCB in the office userinterface

    }

    }

    public class Detachedextends Property{

    public Detached(){

    add the locationCB in the detacheduserinterface

    }

    }


    THE PROBLEM IS:

    when the user adds a new location I want to update the contents of the locationCB and the new location that the user has entered. when i call the updateLocations method in the class Property nothing is really changed in the Flat, Office, Detached interface. What I want is to notify the combobox in the child class to refresh the locations that they contain...

    I AM REALLY SORRY FOR THE HUGE MAIL and I would be around for any justifications...

    THANK YOU VERY VERY MUCH!!!!

  5. #5
    nikosa is offline Member
    Join Date
    Nov 2008
    Posts
    20
    Rep Power
    0

    Default

    The first post is not exactly the same with the second post but they are following the same idea!

  6. #6
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    5

    Default

    needless to say every property has an location attribute. this is the reason why I keep the combobox in the parent class.
    The property objects are part of the model of the data. The subclasses of property are also part of the model. The model must be kept separate from the view of the model displayed to the user. A JComboBox is a view looking at some data in a model. So the JComboBox must not be part of the Property object.

    I googled "model view" and got several references. Most assume you already understand model-view-controller. One that may help is by Burbeck.

  7. #7
    nikosa is offline Member
    Join Date
    Nov 2008
    Posts
    20
    Rep Power
    0

    Default

    Hello zweibieren,

    I am getting a high level idea of what you are talking about so I would like to apologise if I am going to say stupis things in the post. I have not clearly understand the model view controller..

    What I am doing in my project is to have a VIEW object of:

    AddPropertyGUI

    The AddPropertyGUI creates instances of the property types (ex PropertyGUI f = new Flat();, PropertyGUI d = new Detached(), PropertyGUI o = new Office();)

    inside the Flat() object i have a wrapper panel that wraps the characteristics that the property has, say.. road name, road num, location, bedrooms... and returns this panel to be added in the main GUI

    *The location is always wrapped from every property panel.

    Can you suggest any solution or I have a huge problem that cannot be fixed

  8. #8
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    5

    Default

    Relax. The problem can be fixed. But it may be a lot of code reorganization.

    Think of the Property items as pure data.
    It is not absolutely wrong to have them build JPanels,
    but it is leading your design astray.

    Build the screen image from a bunch of JPanels,
    but create each screen JPanel only once.
    Now write code in the application that will
    populate an existing JPanel from the current data items.
    This is the application code; it is not in the Property object
    nor in the JPanel.

  9. #9
    nikosa is offline Member
    Join Date
    Nov 2008
    Posts
    20
    Rep Power
    0

    Default

    Hello I am not sure that I go the idea. Can you give me a very simple code example.
    What I thought was to keep the location Combo Box in a separate class in order to access it from every class and populate it everytime.

    Is there any object that will notify the child objects whenever it is changed?

  10. #10
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    5

    Default

    Suppose our application is an index from state names to two-letter identifiers. (PA for Pennsylvania, OK for Oklahoma, ...)

    Model-View-Controller Pattern

    In this pattern three objects cooperate to change the screen
    when switching to different properties: the App, the Property, and the View.

    Property methods: getName(String id), getID(String name), ...
    View method: showProperty(Property p) ...
    this method changes what is visible in this view
    it gets the data from the Property with getName()... and getID()...
    App method: fetchData(String statename) ...

    In response to user input,
    the App method fetches a data record for the named state.
    It creates a Property object for the new property.
    Finally it changes the view with showProperty(the new Property)



    Is there any object that will notify the child objects whenever it is changed?
    Any class can be made to perform this trick.
    It implements the method addPropertyChangeListener:

    Java Code:
    ArrayList<PropertyChangeListener> pclList 
                = new ArrayList<PropertyChangeListener>();
    
    addPropertyChangeListener(PropertyChangeListener pcl) {pclList.add(pcl);}
    
    type method otherMethod(...) {
        ...
        // property "propertyName" has changed from oldValue to newValue. 
        PropertyChangeEvent pce = new PropertyChangeEvent(this, 
                                  "propertyName", oldValue, newValue); 
        for (PropertyChangeListener pcl : pclList) 
            pcl.propertyChange(pce);
        ...
    }
    A client implements the PropertyChangeListener interface
    and adds itself to the Property object.
    Its propertyChange method is called when the property changes.
    (As is evident from the Property object's code above.)

    Java Code:
    class Client implements PropertyChangeListener {
        Property data;
     
        public Client (...) {
            ...
            data = new Data(...);
            data.add(this);
            ...
        }
        ...
        public void propertyChange(PropertyChangeEvent pce) {
                  ... handle the change
        }
        ...
    
    }

Similar Threads

  1. JComboBox
    By pinks_70986 in forum New To Java
    Replies: 2
    Last Post: 02-19-2009, 05:26 PM
  2. I need help with JComboBox
    By eva21 in forum New To Java
    Replies: 1
    Last Post: 11-28-2008, 10:05 PM
  3. JCombobox
    By daniel50096230 in forum NetBeans
    Replies: 1
    Last Post: 09-21-2008, 12:23 PM
  4. JComboBox
    By Fosters in forum AWT / Swing
    Replies: 0
    Last Post: 08-10-2008, 01:22 PM
  5. jcombobox
    By Freddie in forum AWT / Swing
    Replies: 4
    Last Post: 05-11-2007, 12:48 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
  •