Results 1 to 6 of 6
  1. #1
    rudeboy is offline Member
    Join Date
    Nov 2010
    Posts
    11
    Rep Power
    0

    Default Observable/Observer JComboBox

    Hi all, I hope that you can help me with the following issue:

    I have an JTabbedPane object which has three tabs. Each tab consists of several components, including an JComboBox object. All tabs are similar.
    I need to make these combos are observable/observer objects among them. That is, for instance, when one of them removes an item, everyone should see this change and update itself. Thus, this means that each combo must be observable/observer object.

    How can I do for each combo can detects changes and updates its own item list? :confused:

    Sorry for my English :(

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Does each combo box hold the exact same items? If so, the solution is easy -- give all JComboBoxes the same model.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    give all JComboBoxes the same model.
    Um, that'll lock them all to the same selection, which is probably not what's wanted.

    Just guessing here, but I think a custom ComboBoxModel with the data held in a static field might be the best solution. It might even be trivial (or not!) to copy DefaultComboBoxModel and convert some members to static.

    db

    edit But read the next response too
    Last edited by DarrylBurke; 11-27-2010 at 06:43 AM.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Another approach could be to use a separate ListModel -- possibly a DefaultListModel --and give each combo an extended DefaultComboBoxModel that implements ListDataListener to listen for changes to the shared model and update itself. JComboBox itself implements ListDataListener, but see the doc comments for the three methods it implements.

    All changes such as removing an item would be invoked on the shared ListModel.

    On second thought, this is probably a better approach (as in more OOPS) than my knee-jerk first suggestion.

    db

  5. #5
    rudeboy is offline Member
    Join Date
    Nov 2010
    Posts
    11
    Rep Power
    0

    Default

    Thanks you for your answer, Fubarable!

    I could accomplish what I wanted using your suggest! :)

    Thanks you again!
    Regards.

  6. #6
    rudeboy is offline Member
    Join Date
    Nov 2010
    Posts
    11
    Rep Power
    0

    Default

    db, thanks you for answering!

    I'll consider your advice also, but now I could solve the mentioned issue.

    Regards


    Quote Originally Posted by Darryl.Burke View Post
    Another approach could be to use a separate ListModel -- possibly a DefaultListModel --and give each combo an extended DefaultComboBoxModel that implements ListDataListener to listen for changes to the shared model and update itself. JComboBox itself implements ListDataListener, but see the doc comments for the three methods it implements.

    All changes such as removing an item would be invoked on the shared ListModel.

    On second thought, this is probably a better approach (as in more OOPS) than my knee-jerk first suggestion.

    db

Similar Threads

  1. Pattern Observer
    By brontolo in forum Advanced Java
    Replies: 6
    Last Post: 09-02-2010, 03:40 PM
  2. Have I understood how the observer pattern works?
    By kiregad in forum New To Java
    Replies: 1
    Last Post: 05-16-2010, 09:41 AM
  3. Problem with observer pattern
    By livnihai in forum New To Java
    Replies: 5
    Last Post: 10-11-2009, 03:34 PM

Tags for this Thread

Posting Permissions

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