I am having difficulty updating my JTable from a JButton embedded in a panel separate from the panel holding the JTable. In other words, how do I communicate my data updates to the TableModel from a class separate from the JTable. My JButton takes the origin and destination airports from two JComboBox respectively and feeds the information into my criteriaFind() method. I don't want to create an inner class with the panel containing the JButton/JComboBox panel, since this would bloat my JTable panel code. I want to keep these two panels separate. When I embed a sample JButton, by itself, in my JTable panel, I can update the TableModel directly and update the JTable easily. Do I need to create some sort of Mediator, Facade or third class to communicate from my button/combo panel to the table?
If you are not planning to expose the JTable to the other components in your app, then the only reasonable way to do it is to add methods in its container (panel) that update the table, which can be called by the other components. I personally had a Client (JFrame), which contained a tabbed pane with two panels FlightSearchPanel, and FlightInfoPanel. The Client acted as the Mediator of these two and some other components. So when the search button is pressed, an anonymous ActionListener implemented in Client would perform the search, and call a method setFlights() in FlightSearchPanel, which was there delegated to the actual TableModel. I hope I was clear enough and you got my idea... Hope this helps!!! Benjam�n
Joined: Jul 30, 2001
I just figured it out based on your reply!! What I did was use the JFrame, which holds instances of all the GUI objects -- the combo boxes/button panel and the table panel -- as the Mediator. I created an instance of my table model in the JFrame and then passed that instance as a parameter to each of the instantiations of the two panels. In the panels themselves, I passed the table model as a parameter in the constructor of the panels. For the panel holding the table, I assigned the parameter value to the model instance that called my updating method in the table model. I hope this makes sense. In any case, it worked and I was able to keep all my panels and frame as separate, free-standing classes.