Master Detail with JCombo & JList?

Discussion in 'Java' started by mttc, Jun 19, 2009.

  1. mttc

    mttc Guest

    JComboBox as master, JList as detail. both components fill the model
    from DB.
    How make it work, when i change the master, the detail update list
    automatically.
    comment: I prepare load all records of details at once, there is few
    records. but I not see on DefaultListModel any method for filter view
    according the master key
     
    mttc, Jun 19, 2009
    #1
    1. Advertising

  2. mttc

    Albert Guest

    mttc a écrit :
    > JComboBox as master, JList as detail. both components fill the model
    > from DB.
    > How make it work, when i change the master, the detail update list
    > automatically.
    > comment: I prepare load all records of details at once, there is few
    > records. but I not see on DefaultListModel any method for filter view
    > according the master key


    Yes, you have to do it. You can use a hashmap wich maps each master key
    to an arraylist of "details". And then fill the JList from the good
    arraylist each time the jcombobox value change (actionEvent).
     
    Albert, Jun 19, 2009
    #2
    1. Advertising

  3. mttc

    mttc Guest

    On Jun 19, 3:33 pm, Albert <> wrote:
    > mttc a écrit :
    >
    > > JComboBox as master, JList as detail. both components fill the model
    > > from DB.
    > > How make it work, when i change the master, the detail update list
    > > automatically.
    > > comment: I prepare load all records of details at once, there is few
    > > records. but I not see on DefaultListModel any method for filter view
    > > according the master key

    >
    > Yes, you have to do it. You can use a hashmap wich maps each master key
    > to an arraylist of "details". And then fill the JList from the good
    > arraylist each time the jcombobox value change (actionEvent).


    have some link for this?
     
    mttc, Jun 19, 2009
    #3
  4. mttc wrote:
    > JComboBox as master, JList as detail. both components fill the model
    > from DB.
    > How make it work, when i change the master, the detail update list
    > automatically.
    > comment: I prepare load all records of details at once, there is few
    > records. but I not see on DefaultListModel any method for filter view
    > according the master key


    Of course. There's no reason for (Default)ListModel to provide filter
    methods. There are many ways to implement a filter but it depends on
    interpretation, the concrete situation and the design of the
    application, of course.

    A simple approach could look like the following:

    interface Entity {
    int getParentId();
    }

    class DetailsListModel extends AbstractListModel {
    private List<Entity> details;
    private List<Entity> data;
    private int filterId;

    public DetailsListModel(List<Entity> details) {
    this.details = details;
    filter();
    }

    public void setFilterId(int id) {
    this.filterId = id;
    filter();
    }

    private void filter() {
    if ( filterId == 0 )
    data = details;
    else {
    data = new ArrayList<Entity>();
    // add matching elements to data
    }
    fireContentsChanged(this, 0, data.size());
    }

    // implement abstract methods based on data
    // getSize
    // getElementAt
    }

    To get a reusable filter one could implement an observable filter class
    that uses Comparator or bean introspection. A ListModel can then use and
    observe an instance of this filter class.

    Bye
    Michael
     
    Michael Rauscher, Jun 23, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Doug McCann
    Replies:
    1
    Views:
    1,106
    William Brogden
    Aug 5, 2004
  2. Replies:
    4
    Views:
    2,661
  3. asmj
    Replies:
    0
    Views:
    523
  4. Arun Kumar Menon

    Master Detail detail

    Arun Kumar Menon, Aug 6, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    218
    Arun Kumar Menon
    Aug 6, 2003
  5. Frederik Borg

    ASp.net data binding: Master-detail-detail

    Frederik Borg, Jun 9, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    380
    Frederik Borg
    Jun 9, 2006
Loading...

Share This Page