Reputation: 14361
This is a more general question: I have a quite complicated files table in swing (the data model is not trivial) , and when when the user clicks on an entry , two other view components need to change - File statistics view , and the file content view - both on the same screen.
One options is to have all these components class definitions in the same file , and then have the reference each other - But this will make a very messy code.
The other option I could think of is to pass the statistics and content components to the table object , and have him use it - but that will render the table not - reusable anywhere else.
I'm sure there's a better way to do it - what would you recommend to do?
Option 1:
class MyPanel extends JPanel{
private MyTable table;
private MyFileViewer fv;
private MyFileStats stats;
class MyTable {
addMouseListener({ ... fv.update(); stats.update(); })
}
class MyFileViewer{...}
class MyFileStats{...}
}
Options 2:
class MyTable {
MyTable(MyFileViewer fv, MyFileStats stats) { ...
addMouseListener({fv.update, stats.update ... }
}
}
Upvotes: 3
Views: 203
Reputation: 205785
Option 3: The Model–View–Controller pattern, as discussed here and in this outline. MVC hinges on the observer pattern. Instead of having each view update the others, arrange for each view to register itself as a listener to your data model. When the model changes, each view updates itself accordingly.
Upvotes: 5