Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

populate JComboBox and MVC

 
chong zhen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. How to populate JComboBox in the GUI. I think we should query the database to get the origin and destination arrays other sneaking to take a look at database. Am I right?
2. in order to achieve the above, how to decouple the view and the model? Because in the view I need the reference of data (the model ) to populate the JComboBox.
I appreciate your help very much.
chong
[ March 03, 2003: Message edited by: chong zhen ]
 
lev grevnin
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what i had:
MODEL: has methods to work with database (search, book, etc.). Has a reference to view. When any of these methods (search, etc.) are called, at the end of the methods there is somethifn like view.update(). This updates the view with the new info.
VIEW: has reference to Controller. Asks controller to build certain GUI widges for it. In our case let's take a JComboBox. So inside Model.build() (a method called in the beginning in order to initialize the GUI) we can have for example: originJComboBox = controller.getOriginJComboBox();
CONTROLLER: used by the View as in above. Controller has a reference to the model in order to obtain needed information. For example, when originJComboBox = controller.getOriginJComboBox(); call is made in the View, the Controller goes to the model, queries the database, populates the newly created JComboBox with origin airports and returns this JComboBox ready for use by the view!!
So now, on the main GUI screen we have an origin JComboBox with all the origin flights populated. We also need a button to start the search (this button was also created by the controller and returned to the view. All controls like text fields and buttons and JComboBoxes were created by the controller and given by it to the view). When this button is clicked it will go to the model (controller gave a reference of the model to this button when it was returning this newly created button to the view in the view.build())
hope i helped you.
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

So inside Model.build() (a method called in the beginning in order to initialize the GUI) we can have for example: originJComboBox = controller.getOriginJComboBox();

I don't think this is a good idea. The Model should not know anything about the controller, and it certainly doesn't need to know how the Views are built or what they consist of. The idea is to have one Model that can provide data to multiple views, -- when a new View is added, it can register itself as a model listener and thus receive the existing broadcast messages from the Model and update itself. So when a View is added, only view itself needs to change, while the Model and Controller stay the same. In your design, all three parts of MVC will need to change if you add or modify a View.
Eugene.
[ March 03, 2003: Message edited by: Eugene Kononov ]
 
chong zhen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks lev.
That's the idea I have so far. But after reading the posts from other people, I am confused and have doubts about if it's really MVC approach.
regards,
chong
 
chong zhen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Eugene Kononov:

The idea is to have one Model that can provide data to multiple views, -- when a new View is added, it can register itself as a model listener and thus receive the existing broadcast messages from the Model and update itself. So when a View is added, only view itself needs to change, while the Model and Controller stay the same.


Hello Eugene,
Could you please explain your idea more?
My question is: when GUI first starts, the JCombobox should be already populated, and furthermore, I can't find any action to do populating thing--so it seems hard to do view decoupling with controller. But for the JTable, I can use the search button click action to get it populated. Hope you understand what I mean.
regards,
chong
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

My question is: when GUI first starts, the JCombobox should be already populated, and furthermore, I can't find any action to do populating thing--so it seems hard to do view decoupling with controller.

When you client starts, your application model should be instantiated. At that time, invoke a business method in your model to get the origin/destination airports from the database, and let the View update itself when the Model broadcasts the results. I actually didn't have to do it, because I implemented the dynamic mode switch in my client GUI, and nothing would be populated until the user specifies the database.
Eugene.
[ March 05, 2003: Message edited by: Eugene Kononov ]
 
Qusay Jaafar
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is your mark reduced?
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

is your mark reduced?

Huh? If this is a question about our bartender, then the answer is no, Mark is more likely to be promoted to sheriff. If this is a question about the certification result, and if you are asking me, then the answer is yes, I got 151/155.
Eugene.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic