I am student working on a personal java project (read: NOT homework) with a large GUI component and attempting to follow a MVC architecture. I am building up a complicated view using many nested JPanels with various layouts. The actionListeners for buttons in these panels are/will be implemented in a controller class that contains a reference to the model and the view. Each panel only contains a reference to its immediate child panels,so as it stands right now when the controller needs to update something in one of the deeply nested panels, it requires code like this:
view.getCardLayoutPanel().getReceptionPanel().getOrderContentsPanel().updateTable(tableModel);
which seems less than ideal. Some more pseudoish code of the whole system in question is below. I have omitted simple get methods from the code here, but all of my GUI component classes include get methods that return the reference to the child panels that they directly own.
How can this situation be improved while still obeying MVC?
The controller:
public class Controller implements ActionListener {
private View view;
public Controller(View view, Model model){
this.view = view;
this.model = model;
}
private void updateOrderTable() {
TableModel tableModel; //= **call model method to query db and generate a TableModel**
// This looks bad
view.getCardLayoutPanel().getReceptionPanel().getOrderContentsPanel().updateTable(tableModel);
}
}
The View:
public class View {
// Setup stuff
JFrame mainFrame = new JFrame();
myCardLayoutPanel = new MyCardLayoutPanel ();\
// add cardLayoutPanel to mainframe
}
Next layer:
public class MyCardLayoutPanel extends JPanel {
receptionPanel = new ReceptionPanel();
dataEntryPanel = new DataEntryPanel();
// etc
// Add all the panels to the layout
}
One more intermediate panel, which I will omit, follows the same pattern...and finally, the JPanel with the table in it.
public class OrderContentsPanel extends JPanel {
private JTable table;
private JScrollPane scrollpane;
public OrderContentsPanel() {
setLayout(new BorderLayout());
scrollPane = newJScrollPane(table);
add("Center", scrollPane) //
}
public void updateTable(TableModel tablemodel) {
table.setModel(tableModel);
}
}