• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to synchronize the data in two panels?

 
Qunfeng Wang
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In panel A I have a comboBox, the comboBox data is determined by the operation in panel B. When I create an item in B, the comboBox in A should update himself. How to do this?

Thanks in advance.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like an java.util.Observer/Observable pattern. The combobox (or rather the model it's based on) would implement Observer, and the component that creates new items in panel b (or rather, its model) would extend Observable. Don't forget to add the Observer to the Observable by calling addObserver.

If you want to get fancier, you could define your own event, but it seems like overkill in this case.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't want to get that fancy, note that ComboBoxModel (er, make that MutableComboBoxModel) has methods addElement and removeElement, so... if some code *knows* the model, it can update the JComboBox widget easily. As always, remember that the distinction "panel a" versus "panel b" is a false one. My favorite analogy is a fish in an aquarium. When a hand dips into the water, the fish sees only five distinct digits working together and wonders "how do these separate things communicate?".
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maybe this is all you need
cbo2.setModel(cbo1.getModel());
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael Dunn:
maybe this is all you need
cbo2.setModel(cbo1.getModel());


Hmm... the OP didn't mention a *pair* of combos, just that an operation in one place should cause the combo model to change.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> the OP didn't mention a *pair* of combos,

yep, don't know how I read that into it
(perhaps apres Christmas party eyes)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!